mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
detect numeric array when keys are out of order (#332)
This commit is contained in:
parent
567fbb9965
commit
500472808c
@ -420,9 +420,8 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
protected function validateIn($field, $value, $params)
|
protected function validateIn($field, $value, $params)
|
||||||
{
|
{
|
||||||
$isAssoc = array_values($params[0]) !== $params[0];
|
if ($this->isAssociativeArray($params[0])) {
|
||||||
if ($isAssoc) {
|
$params[0] = array_keys($params[0]);
|
||||||
$params[0] = array_keys($params[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$strict = false;
|
$strict = false;
|
||||||
@ -443,8 +442,7 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
protected function validateListContains($field, $value, $params)
|
protected function validateListContains($field, $value, $params)
|
||||||
{
|
{
|
||||||
$isAssoc = array_values($value) !== $value;
|
if ($this->isAssociativeArray($value)) {
|
||||||
if ($isAssoc) {
|
|
||||||
$value = array_keys($value);
|
$value = array_keys($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1566,4 +1564,9 @@ class Validator
|
|||||||
$me->mapFieldRules($field, $rules[$field]);
|
$me->mapFieldRules($field, $rules[$field]);
|
||||||
}, array_keys($rules));
|
}, array_keys($rules));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isAssociativeArray($input){
|
||||||
|
//array contains at least one key that's not an can not be cast to an integer
|
||||||
|
return count(array_filter(array_keys($input), 'is_string')) > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ use Valitron\Validator;
|
|||||||
|
|
||||||
class ValidateTest extends BaseTestCase
|
class ValidateTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testValidWithNoRules()
|
public function testValidWithNoRules()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester'));
|
$v = new Validator(array('name' => 'Chester Tester'));
|
||||||
@ -2920,6 +2921,35 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v->rule('optional', 'address');
|
$v->rule('optional', 'address');
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @see https://github.com/vlucas/valitron/issues/332
|
||||||
|
*/
|
||||||
|
public function testInRuleSearchesValuesForNumericArray(){
|
||||||
|
|
||||||
|
$v = new Valitron\Validator(array('color' => 'purple'));
|
||||||
|
|
||||||
|
$v->rules(array(
|
||||||
|
'in' => array(
|
||||||
|
array('color', array(3=>'green', 2=>'purple'))
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInRuleSearchesKeysForAssociativeArray(){
|
||||||
|
$v = new Valitron\Validator(array('color' => 'purple'));
|
||||||
|
|
||||||
|
$v->rules(array(
|
||||||
|
'in' => array(
|
||||||
|
array('color', array('c-3'=>'green', 'c-2'=>'purple'))
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sampleFunctionCallback($field, $value, array $params)
|
function sampleFunctionCallback($field, $value, array $params)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user