mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Add support for associative arrays using 'in'
This commit is contained in:
parent
f0e08fbc06
commit
5dc7fd9fb3
@ -226,6 +226,10 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
protected function validateIn($field, $value, $params)
|
protected function validateIn($field, $value, $params)
|
||||||
{
|
{
|
||||||
|
$isAssoc = array_values($params[0]) !== $params[0];
|
||||||
|
if($isAssoc) {
|
||||||
|
$params[0] = array_keys($params[0]);
|
||||||
|
}
|
||||||
return in_array($value, $params[0]);
|
return in_array($value, $params[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -182,6 +182,17 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInValidAssociativeArray()
|
||||||
|
{
|
||||||
|
$v = new Validator(array('color' => 'green'));
|
||||||
|
$v->rule('in', 'color', array(
|
||||||
|
'red' => 'Red',
|
||||||
|
'green' => 'Green',
|
||||||
|
'blue' => 'Blue'
|
||||||
|
));
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
public function testInInvalid()
|
public function testInInvalid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('color' => 'yellow'));
|
$v = new Validator(array('color' => 'yellow'));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user