From 152767d81e79a138d27dab988318f8a0628598dc Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Mon, 15 Aug 2022 16:33:14 +0800 Subject: [PATCH 1/5] fix: add a new param in processRules and getErrorMessage function to judge if validation fields have asterisk --- system/Validation/Validation.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 7e4a55db55cf..ed370a2f5c08 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -168,7 +168,7 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup if (strpos($field, '*') !== false) { // Process multiple fields foreach ($values as $dotField => $value) { - $this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data); + $this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data, $field); } } else { // Process single field @@ -201,10 +201,17 @@ public function check($value, string $rule, array $errors = []): bool * * @param array|string $value * @param array|null $rules - * @param array $data + * @param array $data The array of data to validate, with `DBGroup`. + * @param string|null $originalField The original asterisk field name like "foo.*.bar". */ - protected function processRules(string $field, ?string $label, $value, $rules = null, ?array $data = null): bool - { + protected function processRules( + string $field, + ?string $label, + $value, + $rules = null, + ?array $data = null, + ?string $originalField = null + ): bool { if ($data === null) { throw new InvalidArgumentException('You must supply the parameter: data.'); } @@ -333,7 +340,8 @@ protected function processRules(string $field, ?string $label, $value, $rules = $field, $label, $param, - (string) $value + (string) $value, + $originalField ); return false; @@ -706,13 +714,21 @@ public function setError(string $field, string $error): ValidationInterface * * @param string|null $value The value that caused the validation to fail. */ - protected function getErrorMessage(string $rule, string $field, ?string $label = null, ?string $param = null, ?string $value = null): string - { + protected function getErrorMessage( + string $rule, + string $field, + ?string $label = null, + ?string $param = null, + ?string $value = null, + ?string $originalField = null + ): string { $param ??= ''; // Check if custom message has been defined by user if (isset($this->customErrors[$field][$rule])) { $message = lang($this->customErrors[$field][$rule]); + } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) { + $message = lang($this->customErrors[$originalField][$rule]); } else { // Try to grab a localized version of the message... // lang() will return the rule name back if not found, From 9888bba6143194a7a576d6e2665edd22eb776718 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Mon, 15 Aug 2022 16:41:45 +0800 Subject: [PATCH 2/5] add : add a description to the changelog in v4.2.5. --- user_guide_src/source/changelogs/v4.2.5.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelogs/v4.2.5.rst b/user_guide_src/source/changelogs/v4.2.5.rst index a670705e4153..e4c45e70cfda 100644 --- a/user_guide_src/source/changelogs/v4.2.5.rst +++ b/user_guide_src/source/changelogs/v4.2.5.rst @@ -14,6 +14,7 @@ BREAKING - The method signature of ``BaseConnection::tableExists()`` has been changed. A second optional parameter ``$cached`` was added. This directs whether to use cache data or not. Default is ``true``, use cache data. - The abstract method signature of ``BaseBuilder::_listTables()`` has been changed. A second optional parameter ``$tableName`` was added. Providing a table name will generate SQL listing only that table. +- The method signature of ``Validation.php::processRules()`` and ``Validation.php::getErrorMessage()`` have been changed. Both of these methods add new ``$originalField`` parameter. Enhancements ************ From 574feb7d86d1fb982317ef25c867639ffcd1c3ef Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 17 Aug 2022 02:27:36 +0800 Subject: [PATCH 3/5] changelog: change changelog --- user_guide_src/source/changelogs/v4.2.5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.2.5.rst b/user_guide_src/source/changelogs/v4.2.5.rst index e4c45e70cfda..eb9c8f7ec643 100644 --- a/user_guide_src/source/changelogs/v4.2.5.rst +++ b/user_guide_src/source/changelogs/v4.2.5.rst @@ -14,7 +14,7 @@ BREAKING - The method signature of ``BaseConnection::tableExists()`` has been changed. A second optional parameter ``$cached`` was added. This directs whether to use cache data or not. Default is ``true``, use cache data. - The abstract method signature of ``BaseBuilder::_listTables()`` has been changed. A second optional parameter ``$tableName`` was added. Providing a table name will generate SQL listing only that table. -- The method signature of ``Validation.php::processRules()`` and ``Validation.php::getErrorMessage()`` have been changed. Both of these methods add new ``$originalField`` parameter. +- The method signature of ``Validation::processRules()`` and ``Validation::getErrorMessage()`` have been changed. Both of these methods add new ``$originalField`` parameter. Enhancements ************ From 964fe7183d9a8b70669c537d41eabdbc0df6e11e Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Sat, 20 Aug 2022 01:31:40 +0800 Subject: [PATCH 4/5] test: add test for validation error with asterisk field. --- tests/system/Validation/ValidationTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index c6339139044d..1d2049a9d965 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -463,6 +463,28 @@ public function testRunGroupWithCustomErrorMessage(): void ], $this->validation->getErrors()); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/6245 + */ + public function testRunWithCustomErrorsAndAsteriskField(): void + { + $data = [ + 'foo' => [ + ['bar' => null], + ['bar' => null], + ] + ]; + $this->validation->setRules( + ['foo.*.bar' => ['label' => 'foo bar', 'rules' => 'required']], + ['foo.*.bar' => ['required' => 'Required']] + ); + $this->validation->run($data); + $this->assertSame([ + 'foo.0.bar' => 'Required', + 'foo.1.bar' => 'Required' + ], $this->validation->getErrors()); + } + /** * @dataProvider rulesSetupProvider * From 512f2539516a521e15cc3e42a09906c6accf0690 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Mon, 22 Aug 2022 23:59:00 +0800 Subject: [PATCH 5/5] fix: cs-fix --- tests/system/Validation/ValidationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 1d2049a9d965..5a48615b6c05 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -472,7 +472,7 @@ public function testRunWithCustomErrorsAndAsteriskField(): void 'foo' => [ ['bar' => null], ['bar' => null], - ] + ], ]; $this->validation->setRules( ['foo.*.bar' => ['label' => 'foo bar', 'rules' => 'required']], @@ -481,7 +481,7 @@ public function testRunWithCustomErrorsAndAsteriskField(): void $this->validation->run($data); $this->assertSame([ 'foo.0.bar' => 'Required', - 'foo.1.bar' => 'Required' + 'foo.1.bar' => 'Required', ], $this->validation->getErrors()); }