Skip to content

Commit 8baf85f

Browse files
committed
fix: custom validation error is cleared when calling setRule() twice
1 parent ca9d244 commit 8baf85f

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

system/Validation/Validation.php

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

402-
$this->setRules($ruleSet + $this->getRules());
402+
$this->setRules($ruleSet + $this->getRules(), $this->customErrors);
403403

404404
return $this;
405405
}

tests/system/Validation/StrictRules/ValidationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,33 @@ public function testRunWithCustomErrors(): void
221221
$this->assertSame('No. Not a number.', $this->validation->getError('bar'));
222222
}
223223

224+
/**
225+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6239
226+
*/
227+
public function testSetRuleWithCustomErrors(): void
228+
{
229+
$data = [
230+
'foo' => 'notanumber',
231+
'bar' => 'notanumber',
232+
];
233+
$this->validation->setRule(
234+
'foo',
235+
'Foo',
236+
['foo' => 'is_numeric'],
237+
['is_numeric' => 'Nope. Not a number.']
238+
);
239+
$this->validation->setRule(
240+
'bar',
241+
'Bar',
242+
['bar' => 'is_numeric'],
243+
['is_numeric' => 'Nope. Not a number.']
244+
);
245+
$this->validation->run($data);
246+
247+
$this->assertSame('Nope. Not a number.', $this->validation->getError('foo'));
248+
$this->assertSame('Nope. Not a number.', $this->validation->getError('bar'));
249+
}
250+
224251
public function testCheck(): void
225252
{
226253
$this->assertFalse($this->validation->check('notanumber', 'is_numeric'));

tests/system/Validation/ValidationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,33 @@ public function testRunWithCustomErrors(): void
324324
$this->assertSame('No. Not a number.', $this->validation->getError('bar'));
325325
}
326326

327+
/**
328+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6239
329+
*/
330+
public function testSetRuleWithCustomErrors(): void
331+
{
332+
$data = [
333+
'foo' => 'notanumber',
334+
'bar' => 'notanumber',
335+
];
336+
$this->validation->setRule(
337+
'foo',
338+
'Foo',
339+
['foo' => 'is_numeric'],
340+
['is_numeric' => 'Nope. Not a number.']
341+
);
342+
$this->validation->setRule(
343+
'bar',
344+
'Bar',
345+
['bar' => 'is_numeric'],
346+
['is_numeric' => 'Nope. Not a number.']
347+
);
348+
$this->validation->run($data);
349+
350+
$this->assertSame('Nope. Not a number.', $this->validation->getError('foo'));
351+
$this->assertSame('Nope. Not a number.', $this->validation->getError('bar'));
352+
}
353+
327354
public function testCheck(): void
328355
{
329356
$this->assertFalse($this->validation->check('notanumber', 'is_numeric'));

0 commit comments

Comments
 (0)