added support for nested arrays in validateEquals and validateDifferent validation methods

This commit is contained in:
Tom Breese 2018-02-04 13:24:47 -05:00
parent 1712b04f16
commit 7c94b2ae5a
2 changed files with 34 additions and 6 deletions

View File

@ -173,9 +173,9 @@ class Validator
*/
protected function validateEquals($field, $value, array $params)
{
$field2 = $params[0];
return isset($this->_fields[$field2]) && $value == $this->_fields[$field2];
// extract the second field value, this accounts for nested array values
list($field2Value, $multiple) = $this->getPart($this->_fields, explode('.', $params[0]));
return isset($field2Value) && $value == $field2Value;
}
/**
@ -189,9 +189,9 @@ class Validator
*/
protected function validateDifferent($field, $value, array $params)
{
$field2 = $params[0];
return isset($this->_fields[$field2]) && $value != $this->_fields[$field2];
// extract the second field value, this accounts for nested array values
list($field2Value, $multiple) = $this->getPart($this->_fields, explode('.', $params[0]));
return isset($field2Value) && $value != $field2Value;
}
/**

View File

@ -1271,6 +1271,34 @@ class ValidateTest extends BaseTestCase
$v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'), true);
$this->assertTrue($v2->validate());
}
public function testNestedEqualsValid()
{
$v = new Validator(array('foo' => array('one' => 'bar', 'two' => 'bar')));
$v->rule('equals', 'foo.one', 'foo.two');
$this->assertTrue($v->validate());
}
public function testNestedEqualsInvalid()
{
$v = new Validator(array('foo' => array('one' => 'bar', 'two' => 'baz')));
$v->rule('equals', 'foo.one', 'foo.two');
$this->assertFalse($v->validate());
}
public function testNestedDifferentValid()
{
$v = new Validator(array('foo' => array('one' => 'bar', 'two' => 'baz')));
$v->rule('different', 'foo.one', 'foo.two');
$this->assertTrue($v->validate());
}
public function testNestedDifferentInvalid()
{
$v = new Validator(array('foo' => array('one' => 'baz', 'two' => 'baz')));
$v->rule('different', 'foo.one', 'foo.two');
$this->assertFalse($v->validate());
}
}
function sampleFunctionCallback($field, $value, array $params) {