From eace42656adafe9f4a9a7c12be64b1e578d1d0f3 Mon Sep 17 00:00:00 2001 From: Mark Cahill Date: Mon, 8 Sep 2014 12:34:46 -0400 Subject: [PATCH 1/2] Validate an array of discrete values --- src/Valitron/Validator.php | 5 +++++ tests/Valitron/ValidateTest.php | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 57ba39e..e6b7e16 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -803,6 +803,11 @@ class Validator protected function getPart($data, $identifiers) { + // Catches the case where the field is an array of discrete values + if (is_array($identifiers) && count($identifiers) === 0) { + return array($data, false); + } + $identifier = array_shift($identifiers); // Glob match diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d9c006d..b954a88 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -308,6 +308,13 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testForeachDiscreteValues() + { + $v = new Validator(array('values' => array(5, 10, 15, 20, 25))); + $v->rule('integer', 'values.*'); + $this->assertTrue($v->validate()); + } + public function testForeachArrayAccess() { $v = new Validator(array('settings' => array( From ec0543be1024da7fe8f28af9d9976bcfc0c2131b Mon Sep 17 00:00:00 2001 From: Mark Cahill Date: Mon, 8 Sep 2014 15:07:49 -0400 Subject: [PATCH 2/2] Tests for associative arrays --- tests/Valitron/ValidateTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index b954a88..16bed82 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -315,6 +315,28 @@ class ValidateTest extends BaseTestCase $this->assertTrue($v->validate()); } + public function testForeachAssocValues() + { + $v = new Validator(array('values' => array( + 'foo' => 5, + 'bar' => 10, + 'baz' => 15 + ))); + $v->rule('integer', 'values.*'); + $this->assertTrue($v->validate()); + } + + public function testForeachAssocValuesFails() + { + $v = new Validator(array('values' => array( + 'foo' => 5, + 'bar' => 10, + 'baz' => 'faz' + ))); + $v->rule('integer', 'values.*'); + $this->assertFalse($v->validate()); + } + public function testForeachArrayAccess() { $v = new Validator(array('settings' => array(