From 8863f7ae8fd03ca31f47104781857d02e31b76ee Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 27 Mar 2020 21:56:30 +0100 Subject: [PATCH 1/6] Fix validation for multi select --- system/Validation/Validation.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index a4e5682c7987..aea184130c39 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -172,7 +172,15 @@ public function run(array $data = null, string $group = null, string $db_group = $value = dot_array_search($rField, $data); - $this->processRules($rField, $rSetup['label'] ?? $rField, $value ?? null, $rules, $data); + if (! is_array($value)) + { + $value = [$value]; + } + + foreach ($value as $val) + { + $this->processRules($rField, $rSetup['label'] ?? $rField, $val ?? null, $rules, $data); + } } return ! empty($this->getErrors()) ? false : true; From c359ad57ad36fde388f83a7d81b2d5d274ee1f4e Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 27 Mar 2020 21:59:22 +0100 Subject: [PATCH 2/6] Add test with no error --- tests/system/Validation/ValidationTest.php | 48 ++++++++++++++++++---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 50afa096e713..0322b09bbadb 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,9 +717,38 @@ 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()); + } } From d8e0d221d0081da05ff1bbba0e16635d0e104bdc Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 27 Mar 2020 22:00:37 +0100 Subject: [PATCH 3/6] Add test with errors --- tests/system/Validation/ValidationTest.php | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 0322b09bbadb..6c7713a3a789 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -751,4 +751,38 @@ public function testRulesForSingleRuleWithAsteriskWillReturnNoError() ->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()); + } } From b73173bec03b036196af42cd517138c4f57091cc Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 27 Mar 2020 22:54:23 +0100 Subject: [PATCH 4/6] Fix issue with single validation with single value --- system/Validation/Validation.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index aea184130c39..6032d638f8da 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -170,16 +170,19 @@ 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); - if (! is_array($value)) + if (is_array($value) && end($fieldNameToken) === '*') { - $value = [$value]; + foreach ($value as $val) + { + $this->processRules($rField, $rSetup['label'] ?? $rField, $val ?? null, $rules, $data); + } } - - foreach ($value as $val) + else { - $this->processRules($rField, $rSetup['label'] ?? $rField, $val ?? null, $rules, $data); + $this->processRules($rField, $rSetup['label'] ?? $rField, $value ?? null, $rules, $data); } } From 6850804d71f107d9c8b1ff2ecedb533cbc2c05fa Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 27 Mar 2020 22:57:35 +0100 Subject: [PATCH 5/6] Add one for test for single value and single validation --- tests/system/Validation/ValidationTest.php | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 6c7713a3a789..002ee5ed1129 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -785,4 +785,31 @@ public function testRulesForSingleRuleWithAsteriskWillReturnError() '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()); + } + + //-------------------------------------------------------------------- } From 7a575dd00830b66852cb3742aa88b5e5ea9703e1 Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 27 Mar 2020 23:04:45 +0100 Subject: [PATCH 6/6] Updated documentations --- user_guide_src/source/libraries/validation.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 ================================================