diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index c629ca4..4be39e3 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -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; } /** diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index adf296e..7f07e5f 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -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) {