From 7c94b2ae5a657d5355c7a64f7c3e7bc7e82cb873 Mon Sep 17 00:00:00 2001 From: Tom Breese Date: Sun, 4 Feb 2018 13:24:47 -0500 Subject: [PATCH] added support for nested arrays in validateEquals and validateDifferent validation methods --- src/Valitron/Validator.php | 12 ++++++------ tests/Valitron/ValidateTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) 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) {