From 5c8b6ff462b29e947965b844236e7e86ba96f534 Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Wed, 18 Mar 2020 23:40:00 +0100 Subject: [PATCH] Fix validation for array input fields --- system/Helpers/array_helper.php | 2 +- tests/system/Validation/ValidationTest.php | 77 ++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index 9c5d8f6e5120..71d3aceeadc1 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -79,7 +79,7 @@ function _array_search_dot(array $indexes, array $array) ? array_shift($indexes) : null; - if (empty($currentIndex) || (! isset($array[$currentIndex]) && $currentIndex !== '*')) + if ((empty($currentIndex) && intval($currentIndex) !== 0) || (! isset($array[$currentIndex]) && $currentIndex !== '*')) { return null; } diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 2d5c33a3f62c..a374153cc5e6 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -614,4 +614,81 @@ public function testTagReplacement() $this->assertEquals($expected, $errors['Username']); } + //-------------------------------------------------------------------- + + /** + * @dataProvider arrayFieldDataProvider + */ + public function testRulesForArrayField($body, $rules, $results) + { + $config = new App(); + $config->baseURL = 'http://example.com'; + + $request = new IncomingRequest($config, new URI(), http_build_query($body), new UserAgent()); + $request->setMethod('post'); + + $this->validation->setRules($rules); + $this->validation->withRequest($request) + ->run($body); + $this->assertEquals($results, $this->validation->getErrors()); + } + + //-------------------------------------------------------------------- + + public function arrayFieldDataProvider() { + return [ + 'all_rules_should_pass' => [ + 'body' => [ + 'foo' => [ + 'a', + 'b', + 'c' + ] + ], + 'rules' => [ + 'foo.0' => 'required|alpha|max_length[2]', + 'foo.1' => 'required|alpha|max_length[2]', + 'foo.2' => 'required|alpha|max_length[2]' + ], + 'results' => [] + ], + 'first_field_will_return_required_error' => [ + 'body' => [ + 'foo' => [ + '', + 'b', + 'c' + ] + ], + 'rules' => [ + 'foo.0' => 'required|alpha|max_length[2]', + 'foo.1' => 'required|alpha|max_length[2]', + 'foo.2' => 'required|alpha|max_length[2]' + ], + 'results' => [ + 'foo.0' => 'The foo.0 field is required.' + ] + ], + 'first_and second_field_will_return_required_and_min_length_error' => [ + 'body' => [ + 'foo' => [ + '', + 'b', + 'c' + ] + ], + 'rules' => [ + 'foo.0' => 'required|alpha|max_length[2]', + 'foo.1' => 'required|alpha|min_length[2]|max_length[4]', + 'foo.2' => 'required|alpha|max_length[2]' + ], + 'results' => [ + 'foo.0' => 'The foo.0 field is required.', + 'foo.1' => 'The foo.1 field must be at least 2 characters in length.', + ] + ] + ]; + } + + //-------------------------------------------------------------------- }