Skip to content

fix: validation custom error with asterisk field #6340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Validation/StrictRules/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down