fix: custom validation error is cleared when calling setRule() twice

This commit is contained in:
kenjis 2022-07-08 10:43:16 +09:00
parent ca9d244517
commit 8baf85fa36
No known key found for this signature in database
GPG Key ID: BD254878922AF198
3 changed files with 55 additions and 1 deletions

View File

@ -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;
}

View File

@ -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'));

View File

@ -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'));