From 8baf85fa3651d8311c18d9c77711495510cf2416 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 8 Jul 2022 10:43:16 +0900 Subject: [PATCH] fix: custom validation error is cleared when calling setRule() twice --- system/Validation/Validation.php | 2 +- .../Validation/StrictRules/ValidationTest.php | 27 +++++++++++++++++++ tests/system/Validation/ValidationTest.php | 27 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index eb91114ca2..2b190ef296 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -399,7 +399,7 @@ class Validation implements ValidationInterface $ruleSet[$field]['errors'] = $errors; } - $this->setRules($ruleSet + $this->getRules()); + $this->setRules($ruleSet + $this->getRules(), $this->customErrors); return $this; } diff --git a/tests/system/Validation/StrictRules/ValidationTest.php b/tests/system/Validation/StrictRules/ValidationTest.php index 1e2e8a97ec..ba0e8c6ec0 100644 --- a/tests/system/Validation/StrictRules/ValidationTest.php +++ b/tests/system/Validation/StrictRules/ValidationTest.php @@ -221,6 +221,33 @@ final class ValidationTest extends CIUnitTestCase $this->assertSame('No. Not a number.', $this->validation->getError('bar')); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/6239 + */ + public function testSetRuleWithCustomErrors(): void + { + $data = [ + 'foo' => 'notanumber', + 'bar' => 'notanumber', + ]; + $this->validation->setRule( + 'foo', + 'Foo', + ['foo' => 'is_numeric'], + ['is_numeric' => 'Nope. Not a number.'] + ); + $this->validation->setRule( + 'bar', + 'Bar', + ['bar' => 'is_numeric'], + ['is_numeric' => 'Nope. Not a number.'] + ); + $this->validation->run($data); + + $this->assertSame('Nope. Not a number.', $this->validation->getError('foo')); + $this->assertSame('Nope. Not a number.', $this->validation->getError('bar')); + } + public function testCheck(): void { $this->assertFalse($this->validation->check('notanumber', 'is_numeric')); diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 05f07aae58..867dd6475d 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -324,6 +324,33 @@ final class ValidationTest extends CIUnitTestCase $this->assertSame('No. Not a number.', $this->validation->getError('bar')); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/6239 + */ + public function testSetRuleWithCustomErrors(): void + { + $data = [ + 'foo' => 'notanumber', + 'bar' => 'notanumber', + ]; + $this->validation->setRule( + 'foo', + 'Foo', + ['foo' => 'is_numeric'], + ['is_numeric' => 'Nope. Not a number.'] + ); + $this->validation->setRule( + 'bar', + 'Bar', + ['bar' => 'is_numeric'], + ['is_numeric' => 'Nope. Not a number.'] + ); + $this->validation->run($data); + + $this->assertSame('Nope. Not a number.', $this->validation->getError('foo')); + $this->assertSame('Nope. Not a number.', $this->validation->getError('bar')); + } + public function testCheck(): void { $this->assertFalse($this->validation->check('notanumber', 'is_numeric'));