diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 7e4a55db55cf..a80c530861a8 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -713,6 +713,8 @@ protected function getErrorMessage(string $rule, string $field, ?string $label = // Check if custom message has been defined by user if (isset($this->customErrors[$field][$rule])) { $message = lang($this->customErrors[$field][$rule]); + } else if (strpos($label, '*') !== false && isset($this->customErrors[$label][$rule])) { + $message = lang($this->customErrors[$label][$rule]); } else { // Try to grab a localized version of the message... // lang() will return the rule name back if not found, diff --git a/tests/system/Validation/StrictRules/ValidationTest.php b/tests/system/Validation/StrictRules/ValidationTest.php index ba0e8c6ec0c5..2cd44c0e87a5 100644 --- a/tests/system/Validation/StrictRules/ValidationTest.php +++ b/tests/system/Validation/StrictRules/ValidationTest.php @@ -359,6 +359,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' => 'required'], + ['foo.*.bar' => ['required' => 'Required']] + ); + $this->validation->run($data); + $this->assertSame([ + 'foo.0.bar' => 'Required', + 'foo.1.bar' => 'Required' + ], $this->validation->getErrors()); + } + /** * @dataProvider rulesSetupProvider *