From e8ab4dc15724bb341efd2ac2b3da69f3fc113081 Mon Sep 17 00:00:00 2001 From: Elizabeth M Smith Date: Fri, 23 Jun 2017 14:01:34 -0400 Subject: [PATCH 1/3] Fixes invalid argument to foreach happens when you have a rule looking for sub items in arrays, but actually pass in a scalar value bombs out with invalid argument to foreach --- src/Valitron/Validator.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 5fc5c84..db79f39 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -889,6 +889,11 @@ class Validator if (is_array($identifiers) && count($identifiers) === 0) { return array($data, false); } + + // Catches the case where the data isn't an array or object + if (is_scalar($data)) { + return array($data, false); + } $identifier = array_shift($identifiers); From 66464f4c51186e0d4ed12b52f5ec0a5cae9c5ad0 Mon Sep 17 00:00:00 2001 From: Elizabeth M Smith Date: Fri, 23 Jun 2017 14:05:47 -0400 Subject: [PATCH 2/3] test for the bug --- tests/Valitron/ValidateTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 490424e..7481394 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -34,6 +34,13 @@ class ValidateTest extends BaseTestCase $v->rule('required', array('name', 'email')); $this->assertFalse($v->validate()); } + + public function testRequiredSubfieldsArrayStringValue() + { + $v = new Validator(array('name' => 'bob')); + $v->rule('required', array('name.*.red')); + $this->assertFalse($v->validate()); + } public function testRequiredValid() { From a7e89cf26cf1e1548e856953e713a851ec61b26d Mon Sep 17 00:00:00 2001 From: Elizabeth M Smith Date: Fri, 23 Jun 2017 14:41:46 -0400 Subject: [PATCH 3/3] fix it correctly... --- src/Valitron/Validator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index db79f39..cf4ec9c 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -892,7 +892,7 @@ class Validator // Catches the case where the data isn't an array or object if (is_scalar($data)) { - return array($data, false); + return array(NULL, false); } $identifier = array_shift($identifiers);