Skip to content

Commit

Permalink
Merge pull request codeigniter4#7691 from kenjis/fix-model-validation
Browse files Browse the repository at this point in the history
fix: [Model] setValidationRule() cannot use with ruleGroup
  • Loading branch information
kenjis committed Jul 16, 2023
2 parents e37b134 + 9192068 commit 70eade0
Show file tree
Hide file tree
Showing 7 changed files with 569 additions and 5 deletions.
19 changes: 16 additions & 3 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ public function skipValidation(bool $skip = true)
}

/**
* Allows to set validation messages.
* Allows to set (and reset) validation messages.
* It could be used when you have to change default or override current validate messages.
*
* @param array $validationMessages Value
Expand Down Expand Up @@ -1376,7 +1376,7 @@ public function setValidationMessage(string $field, array $fieldMessages)
}

/**
* Allows to set validation rules.
* Allows to set (and reset) validation rules.
* It could be used when you have to change default or override current validate rules.
*
* @param array $validationRules Value
Expand All @@ -1401,6 +1401,17 @@ public function setValidationRules(array $validationRules)
*/
public function setValidationRule(string $field, $fieldRules)
{
$rules = $this->validationRules;

// ValidationRules can be either a string, which is the group name,
// or an array of rules.
if (is_string($rules)) {
[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);

$this->validationRules = $rules;
$this->validationMessages = $this->validationMessages + $customErrors;
}

$this->validationRules[$field] = $fieldRules;

return $this;
Expand Down Expand Up @@ -1466,7 +1477,9 @@ public function getValidationRules(array $options = []): array
// ValidationRules can be either a string, which is the group name,
// or an array of rules.
if (is_string($rules)) {
$rules = $this->validation->loadRuleGroup($rules);
[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);

$this->validationMessages = $this->validationMessages + $customErrors;
}

if (isset($options['except'])) {
Expand Down
4 changes: 2 additions & 2 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ protected function loadRuleSets()
* same format used with setRules(). Additionally, check
* for {group}_errors for an array of custom error messages.
*
* @return array
* @return array<int, array> [rules, customErrors]
*
* @throws ValidationException
*/
Expand Down Expand Up @@ -693,7 +693,7 @@ public function loadRuleGroup(?string $group = null)
$this->customErrors = $this->config->{$errorName};
}

return $this->rules;
return [$this->rules, $this->customErrors];
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/_support/Config/Validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Config;

use Config\Validation as ValidationConfig;

class Validation extends ValidationConfig
{
public $signup = [
'id' => 'permit_empty|is_natural_no_zero',
'name' => [
'required',
'min_length[3]',
],
'token' => 'permit_empty|in_list[{id}]',
];
public $signup_errors = [
'name' => [
'required' => 'You forgot to name the baby.',
'min_length' => 'Too short, man!',
],
];
}
27 changes: 27 additions & 0 deletions tests/_support/Models/ValidModelRuleGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Models;

use CodeIgniter\Model;

class ValidModelRuleGroup extends Model
{
protected $table = 'job';
protected $returnType = 'object';
protected $useSoftDeletes = false;
protected $dateFormat = 'int';
protected $allowedFields = [
'name',
'description',
];
protected $validationRules = 'signup';
}
Loading

0 comments on commit 70eade0

Please sign in to comment.