mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
fix: custom validation error is cleared when calling setRule() twice
This commit is contained in:
parent
ca9d244517
commit
8baf85fa36
@ -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;
|
||||
}
|
||||
|
@ -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'));
|
||||
|
@ -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'));
|
||||
|
Loading…
x
Reference in New Issue
Block a user