mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-31 07:01:54 +00:00
Added withData method for immutable reusability
This commit is contained in:
parent
fc6f5c138e
commit
94317636d9
@ -1085,4 +1085,19 @@ class Validator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace data on cloned instance
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $fields
|
||||
* @return Valitron
|
||||
*/
|
||||
public function withData($data, $fields = array())
|
||||
{
|
||||
$clone = clone $this;
|
||||
$clone->reset();
|
||||
$clone->_fields = !empty($fields) ? array_intersect_key($data, array_flip($fields)) : $data;
|
||||
return $clone;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1155,7 +1155,20 @@ class ValidateTest extends BaseTestCase
|
||||
$v = new Validator(array());
|
||||
$v->rule('optional', 'address')->rule('email', 'address');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWithData()
|
||||
{
|
||||
$v = new Validator(array());
|
||||
$v->rule('required', 'name');
|
||||
//validation failed, so must have errors
|
||||
$this->assertFalse($v->validate());
|
||||
$this->assertNotEmpty($v->errors());
|
||||
//create copy with different data
|
||||
$v2 = $v->withData(array('name' => 'Chester Tester'));
|
||||
$this->assertTrue($v2->validate());
|
||||
$this->assertEmpty($v2->errors());
|
||||
}
|
||||
}
|
||||
|
||||
function sampleFunctionCallback($field, $value, array $params) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user