mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-31 07:01:54 +00:00
Merge pull request #161 from vlucas/withdata
Added withData method for immutable reusability
This commit is contained in:
commit
6200b45304
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1156,6 +1156,19 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v->rule('optional', 'address')->rule('email', 'address');
|
$v->rule('optional', 'address')->rule('email', 'address');
|
||||||
$this->assertTrue($v->validate());
|
$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) {
|
function sampleFunctionCallback($field, $value, array $params) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user