mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
add parameter to force in and listContains to look at array keys
This commit is contained in:
parent
500472808c
commit
be8cb9e074
@ -420,7 +420,11 @@ class Validator
|
||||
*/
|
||||
protected function validateIn($field, $value, $params)
|
||||
{
|
||||
if ($this->isAssociativeArray($params[0])) {
|
||||
$forceAsAssociative = false;
|
||||
if (isset($params[2])) {
|
||||
$forceAsAssociative = (bool) $params[2];
|
||||
}
|
||||
if ($this->isAssociativeArray($params[0]) || $forceAsAssociative) {
|
||||
$params[0] = array_keys($params[0]);
|
||||
}
|
||||
|
||||
@ -442,7 +446,12 @@ class Validator
|
||||
*/
|
||||
protected function validateListContains($field, $value, $params)
|
||||
{
|
||||
if ($this->isAssociativeArray($value)) {
|
||||
$forceAsAssociative = false;
|
||||
if (isset($params[2])) {
|
||||
$forceAsAssociative = (bool) $params[2];
|
||||
}
|
||||
|
||||
if ($this->isAssociativeArray($value) || $forceAsAssociative) {
|
||||
$value = array_keys($value);
|
||||
}
|
||||
|
||||
|
||||
@ -2940,7 +2940,7 @@ class ValidateTest extends BaseTestCase
|
||||
}
|
||||
|
||||
public function testInRuleSearchesKeysForAssociativeArray(){
|
||||
$v = new Valitron\Validator(array('color' => 'purple'));
|
||||
$v = new Valitron\Validator(array('color' => 'c-2'));
|
||||
|
||||
$v->rules(array(
|
||||
'in' => array(
|
||||
@ -2948,7 +2948,18 @@ class ValidateTest extends BaseTestCase
|
||||
)
|
||||
));
|
||||
|
||||
$this->assertFalse($v->validate());
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testInRuleSearchesKeysWhenForcedTo(){
|
||||
$v = new Valitron\Validator(array('color' => 2));
|
||||
|
||||
$v->rules(array(
|
||||
'in' => array(
|
||||
array('color', array('3'=>'green', '2'=>'purple'), null, true)
|
||||
)
|
||||
));
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user