Skip to content

Commit 67295eb

Browse files
authored
Merge pull request #7149 from ddevsr/ordering-validation
fix: ordering `Validation` show error by call `setRule()`
2 parents 0970eab + 9f35ec5 commit 67295eb

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

system/Validation/Validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public function setRule(string $field, ?string $label, $rules, array $errors = [
443443
$ruleSet[$field]['errors'] = $errors;
444444
}
445445

446-
$this->setRules($ruleSet + $this->getRules(), $this->customErrors);
446+
$this->setRules(array_merge($this->getRules(), $ruleSet), $this->customErrors);
447447

448448
return $this;
449449
}

tests/system/Validation/ValidationTest.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ public function testSetRuleStoresRule()
102102
], $this->validation->getRules());
103103
}
104104

105+
public function testSetRuleMultipleWithIndividual()
106+
{
107+
$this->validation->setRule('username', 'Username', 'required|min_length[3]');
108+
$this->validation->setRule('password', 'Password', ['required', 'min_length[8]', 'alpha_numeric_punct']);
109+
110+
$this->assertSame([
111+
'username' => [
112+
'label' => 'Username',
113+
'rules' => 'required|min_length[3]',
114+
],
115+
'password' => [
116+
'label' => 'Password',
117+
'rules' => [
118+
'required',
119+
'min_length[8]',
120+
'alpha_numeric_punct',
121+
],
122+
],
123+
], $this->validation->getRules());
124+
}
125+
105126
public function testSetRuleAddsRule()
106127
{
107128
$this->validation->setRules([
@@ -113,14 +134,14 @@ public function testSetRuleAddsRule()
113134
$this->validation->setRule('foo', null, 'foo|foz');
114135

115136
$this->assertSame([
116-
'foo' => [
117-
'label' => null,
118-
'rules' => 'foo|foz',
119-
],
120137
'bar' => [
121138
'label' => null,
122139
'rules' => 'bar|baz',
123140
],
141+
'foo' => [
142+
'label' => null,
143+
'rules' => 'foo|foz',
144+
],
124145
], $this->validation->getRules());
125146
}
126147

@@ -142,6 +163,24 @@ public function testSetRuleOverwritesRule()
142163
], $this->validation->getRules());
143164
}
144165

166+
public function testSetRuleOverwritesRuleReverse()
167+
{
168+
$this->validation->setRule('foo', null, 'foo|foz');
169+
$this->validation->setRules([
170+
'foo' => [
171+
'label' => null,
172+
'rules' => 'bar|baz',
173+
],
174+
]);
175+
176+
$this->assertSame([
177+
'foo' => [
178+
'label' => null,
179+
'rules' => 'bar|baz',
180+
],
181+
], $this->validation->getRules());
182+
}
183+
145184
/**
146185
* @dataProvider setRuleRulesFormatCaseProvider
147186
*

0 commit comments

Comments
 (0)