Merge pull request #237 from mangopeaches/nested-equals-different

added nested array support for validateEquals and validateDifferent
This commit is contained in:
Willem Wollebrants 2018-02-07 17:35:13 +01:00 committed by GitHub
commit 8abb9b7220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 155 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

@ -70,6 +70,36 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate());
}
public function testEqualsBothNull()
{
$v = new Validator(array('foo' => null, 'bar' => null));
$v->rule('equals', 'foo', 'bar');
$this->assertTrue($v->validate());
}
public function testEqualsBothNullRequired()
{
$v = new Validator(array('foo' => null, 'bar' => null));
$v->rule('required', array('foo', 'bar'));
$v->rule('equals', 'foo', 'bar');
$this->assertFalse($v->validate());
}
public function testEqualsBothUnset()
{
$v = new Validator(array('foo' => 1));
$v->rule('equals', 'bar', 'baz');
$this->assertTrue($v->validate());
}
public function testEqualsBothUnsetRequired()
{
$v = new Validator(array('foo' => 1));
$v->rule('required', array('bar', 'baz'));
$v->rule('equals', 'bar', 'baz');
$this->assertFalse($v->validate());
}
public function testDifferentValid()
{
$v = new Validator(array('foo' => 'bar', 'bar' => 'baz'));
@ -84,6 +114,36 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate());
}
public function testDifferentBothNull()
{
$v = new Validator(array('foo' => null, 'bar' => null));
$v->rule('equals', 'foo', 'bar');
$this->assertTrue($v->validate());
}
public function testDifferentBothNullRequired()
{
$v = new Validator(array('foo' => null, 'bar' => null));
$v->rule('required', array('foo', 'bar'));
$v->rule('equals', 'foo', 'bar');
$this->assertFalse($v->validate());
}
public function testDifferentBothUnset()
{
$v = new Validator(array('foo' => 1));
$v->rule('equals', 'bar', 'baz');
$this->assertTrue($v->validate());
}
public function testDifferentBothUnsetRequired()
{
$v = new Validator(array('foo' => 1));
$v->rule('required', array('bar', 'baz'));
$v->rule('equals', 'bar', 'baz');
$this->assertFalse($v->validate());
}
public function testAcceptedValid()
{
$v = new Validator(array('agree' => 'yes'));
@ -1283,6 +1343,95 @@ 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 testNestedEqualsBothNull()
{
$v = new Validator(array('foo' => array('bar' => null, 'baz' => null)));
$v->rule('equals', 'foo.bar', 'foo.baz');
$this->assertTrue($v->validate());
}
public function testNestedEqualsBothNullRequired()
{
$v = new Validator(array('foo' => array('bar' => null, 'baz' => null)));
$v->rule('required', array('foo.bar', 'foo.baz'));
$v->rule('equals', 'foo.bar', 'foo.baz');
$this->assertFalse($v->validate());
}
public function testNestedEqualsBothUnset()
{
$v = new Validator(array('foo' => 'bar'));
$v->rule('equals', 'foo.one', 'foo.two');
$this->assertTrue($v->validate());
}
public function testNestedEqualsBothUnsetRequired()
{
$v = new Validator(array('foo' => 'bar'));
$v->rule('required', array('foo.one', 'foo.two'));
$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());
}
public function testNestedDifferentBothNull()
{
$v = new Validator(array('foo' => array('bar' => null, 'baz' => null)));
$v->rule('different', 'foo.bar', 'foo.baz');
$this->assertTrue($v->validate());
}
public function testNestedDifferentBothNullRequired()
{
$v = new Validator(array('foo' => array('bar' => null, 'baz' => null)));
$v->rule('required', array('foo.bar', 'foo.baz'));
$v->rule('different', 'foo.bar', 'foo.baz');
$this->assertFalse($v->validate());
}
public function testNestedDifferentBothUnset()
{
$v = new Validator(array('foo' => 'bar'));
$v->rule('different', 'foo.bar', 'foo.baz');
$this->assertTrue($v->validate());
}
public function testNestedDifferentBothUnsetRequired()
{
$v = new Validator(array('foo' => 'bar'));
$v->rule('required', array('foo.bar', 'foo.baz'));
$v->rule('different', 'foo.bar', 'foo.baz');
$this->assertFalse($v->validate());
}
}
function sampleFunctionCallback($field, $value, array $params) {