Skip to content

Commit 70eade0

Browse files
authored
Merge pull request #7691 from kenjis/fix-model-validation
fix: [Model] setValidationRule() cannot use with ruleGroup
2 parents e37b134 + 9192068 commit 70eade0

File tree

7 files changed

+569
-5
lines changed

7 files changed

+569
-5
lines changed

system/BaseModel.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ public function skipValidation(bool $skip = true)
13451345
}
13461346

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

13781378
/**
1379-
* Allows to set validation rules.
1379+
* Allows to set (and reset) validation rules.
13801380
* It could be used when you have to change default or override current validate rules.
13811381
*
13821382
* @param array $validationRules Value
@@ -1401,6 +1401,17 @@ public function setValidationRules(array $validationRules)
14011401
*/
14021402
public function setValidationRule(string $field, $fieldRules)
14031403
{
1404+
$rules = $this->validationRules;
1405+
1406+
// ValidationRules can be either a string, which is the group name,
1407+
// or an array of rules.
1408+
if (is_string($rules)) {
1409+
[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);
1410+
1411+
$this->validationRules = $rules;
1412+
$this->validationMessages = $this->validationMessages + $customErrors;
1413+
}
1414+
14041415
$this->validationRules[$field] = $fieldRules;
14051416

14061417
return $this;
@@ -1466,7 +1477,9 @@ public function getValidationRules(array $options = []): array
14661477
// ValidationRules can be either a string, which is the group name,
14671478
// or an array of rules.
14681479
if (is_string($rules)) {
1469-
$rules = $this->validation->loadRuleGroup($rules);
1480+
[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);
1481+
1482+
$this->validationMessages = $this->validationMessages + $customErrors;
14701483
}
14711484

14721485
if (isset($options['except'])) {

system/Validation/Validation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ protected function loadRuleSets()
665665
* same format used with setRules(). Additionally, check
666666
* for {group}_errors for an array of custom error messages.
667667
*
668-
* @return array
668+
* @return array<int, array> [rules, customErrors]
669669
*
670670
* @throws ValidationException
671671
*/
@@ -693,7 +693,7 @@ public function loadRuleGroup(?string $group = null)
693693
$this->customErrors = $this->config->{$errorName};
694694
}
695695

696-
return $this->rules;
696+
return [$this->rules, $this->customErrors];
697697
}
698698

699699
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Config;
13+
14+
use Config\Validation as ValidationConfig;
15+
16+
class Validation extends ValidationConfig
17+
{
18+
public $signup = [
19+
'id' => 'permit_empty|is_natural_no_zero',
20+
'name' => [
21+
'required',
22+
'min_length[3]',
23+
],
24+
'token' => 'permit_empty|in_list[{id}]',
25+
];
26+
public $signup_errors = [
27+
'name' => [
28+
'required' => 'You forgot to name the baby.',
29+
'min_length' => 'Too short, man!',
30+
],
31+
];
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Models;
13+
14+
use CodeIgniter\Model;
15+
16+
class ValidModelRuleGroup extends Model
17+
{
18+
protected $table = 'job';
19+
protected $returnType = 'object';
20+
protected $useSoftDeletes = false;
21+
protected $dateFormat = 'int';
22+
protected $allowedFields = [
23+
'name',
24+
'description',
25+
];
26+
protected $validationRules = 'signup';
27+
}

0 commit comments

Comments
 (0)