Skip to content
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

Fix validation custom error asterisk field #6378

Merged
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
30 changes: 23 additions & 7 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.');
}
Expand Down Expand Up @@ -333,7 +340,8 @@ protected function processRules(string $field, ?string $label, $value, $rules =
$field,
$label,
$param,
(string) $value
(string) $value,
$originalField
);

return false;
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::processRules()`` and ``Validation::getErrorMessage()`` have been changed. Both of these methods add new ``$originalField`` parameter.

Enhancements
************
Expand Down