From 5dc7fd9fb3cd96f78146db252344d1cfb2078694 Mon Sep 17 00:00:00 2001 From: Vance Lucas Date: Fri, 30 Aug 2013 10:31:57 -0500 Subject: [PATCH] Add support for associative arrays using 'in' --- src/Valitron/Validator.php | 4 ++++ tests/Valitron/ValidateTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index e25de20..1ee80ca 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -226,6 +226,10 @@ class Validator */ 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]); } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index b3d5d1b..0c1f41a 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -182,6 +182,17 @@ class ValidateTest extends BaseTestCase $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() { $v = new Validator(array('color' => 'yellow'));