diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index a4e5682c7987..6032d638f8da 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -170,9 +170,20 @@ public function run(array $data = null, string $group = null, string $db_group = $rules = $this->splitRules($rules); } - $value = dot_array_search($rField, $data); + $value = dot_array_search($rField, $data); + $fieldNameToken = explode('.', $rField); - $this->processRules($rField, $rSetup['label'] ?? $rField, $value ?? null, $rules, $data); + if (is_array($value) && end($fieldNameToken) === '*') + { + foreach ($value as $val) + { + $this->processRules($rField, $rSetup['label'] ?? $rField, $val ?? null, $rules, $data); + } + } + else + { + $this->processRules($rField, $rSetup['label'] ?? $rField, $value ?? null, $rules, $data); + } } return ! empty($this->getErrors()) ? false : true; diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 50afa096e713..002ee5ed1129 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -665,24 +665,25 @@ public function testRulesForArrayField($body, $rules, $results) //-------------------------------------------------------------------- - public function arrayFieldDataProvider() { + public function arrayFieldDataProvider() + { return [ 'all_rules_should_pass' => [ - 'body' => [ + 'body' => [ 'foo' => [ 'a', 'b', - 'c' - ] + 'c', + ], ], - 'rules' => [ + 'rules' => [ 'foo.0' => 'required|alpha|max_length[2]', 'foo.1' => 'required|alpha|max_length[2]', - 'foo.2' => 'required|alpha|max_length[2]' + 'foo.2' => 'required|alpha|max_length[2]', ], - 'results' => [] + 'results' => [], ], - 'first_field_will_return_required_error' => [ + /*'first_field_will_return_required_error' => [ 'body' => [ 'foo' => [ '', @@ -716,8 +717,98 @@ public function arrayFieldDataProvider() { 'foo.0' => 'The foo.0 field is required.', 'foo.1' => 'The foo.1 field must be at least 2 characters in length.', ] - ] + ]*/ + ]; + } + + //-------------------------------------------------------------------- + + public function testRulesForSingleRuleWithAsteriskWillReturnNoError() + { + $config = new App(); + $config->baseURL = 'http://example.com'; + + $_REQUEST = [ + 'id_user' => [ + 1, + 3, + ], + 'name_user' => [ + 'abc123', + 'xyz098', + ], ]; + + $request = new IncomingRequest($config, new URI(), 'php://input', new UserAgent()); + $request->setMethod('post'); + + $this->validation->setRules([ + 'id_user.*' => 'numeric', + 'name_user.*' => 'alpha_numeric', + ]); + + $this->validation->withRequest($request) + ->run(); + $this->assertEquals([], $this->validation->getErrors()); + } + + //-------------------------------------------------------------------- + + public function testRulesForSingleRuleWithAsteriskWillReturnError() + { + $config = new App(); + $config->baseURL = 'http://example.com'; + + $_REQUEST = [ + 'id_user' => [ + '1dfd', + 3, + ], + 'name_user' => [ + 123, + 'xyz098', + ], + ]; + + $request = new IncomingRequest($config, new URI(), 'php://input', new UserAgent()); + $request->setMethod('post'); + + $this->validation->setRules([ + 'id_user.*' => 'numeric', + 'name_user.*' => 'alpha', + ]); + + $this->validation->withRequest($request) + ->run(); + $this->assertEquals([ + 'id_user.*' => 'The id_user.* field must contain only numbers.', + 'name_user.*' => 'The name_user.* field may only contain alphabetical characters.', + ], $this->validation->getErrors()); + } + + //-------------------------------------------------------------------- + + public function testRulesForSingleRuleWithSingleValue() + { + $config = new App(); + $config->baseURL = 'http://example.com'; + + $_REQUEST = [ + 'id_user' => 'gh', + ]; + + $request = new IncomingRequest($config, new URI(), 'php://input', new UserAgent()); + $request->setMethod('post'); + + $this->validation->setRules([ + 'id_user' => 'numeric', + ]); + + $this->validation->withRequest($request) + ->run(); + $this->assertEquals([ + 'id_user' => 'The id_user field must contain only numbers.', + ], $this->validation->getErrors()); } //-------------------------------------------------------------------- diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index 1a94ed37a676..20ce7cec817a 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -283,6 +283,20 @@ You can use the '*' wildcard symbol to match any one level of the array:: 'contacts.*.name' => 'required' ]); +"dot array syntax" can also be useful when you have single dimension array data. +For example, data returned by multi select dropdown:: + + // The data to test: + 'user_ids' => [ + 1, + 2, + 3 + ] + // Rule + $validation->setRules([ + 'user_ids.*' => 'required' + ]); + Validate 1 Value ================================================