Skip to content

Commit 071cd04

Browse files
authored
Merge pull request #6378 from ping-yee/fix-validation-custom-error-asterisk-field
Fix validation custom error asterisk field
2 parents 40c69f0 + 512f253 commit 071cd04

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

system/Validation/Validation.php

+23-7
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
168168
if (strpos($field, '*') !== false) {
169169
// Process multiple fields
170170
foreach ($values as $dotField => $value) {
171-
$this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data);
171+
$this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data, $field);
172172
}
173173
} else {
174174
// Process single field
@@ -201,10 +201,17 @@ public function check($value, string $rule, array $errors = []): bool
201201
*
202202
* @param array|string $value
203203
* @param array|null $rules
204-
* @param array $data
204+
* @param array $data The array of data to validate, with `DBGroup`.
205+
* @param string|null $originalField The original asterisk field name like "foo.*.bar".
205206
*/
206-
protected function processRules(string $field, ?string $label, $value, $rules = null, ?array $data = null): bool
207-
{
207+
protected function processRules(
208+
string $field,
209+
?string $label,
210+
$value,
211+
$rules = null,
212+
?array $data = null,
213+
?string $originalField = null
214+
): bool {
208215
if ($data === null) {
209216
throw new InvalidArgumentException('You must supply the parameter: data.');
210217
}
@@ -333,7 +340,8 @@ protected function processRules(string $field, ?string $label, $value, $rules =
333340
$field,
334341
$label,
335342
$param,
336-
(string) $value
343+
(string) $value,
344+
$originalField
337345
);
338346

339347
return false;
@@ -706,13 +714,21 @@ public function setError(string $field, string $error): ValidationInterface
706714
*
707715
* @param string|null $value The value that caused the validation to fail.
708716
*/
709-
protected function getErrorMessage(string $rule, string $field, ?string $label = null, ?string $param = null, ?string $value = null): string
710-
{
717+
protected function getErrorMessage(
718+
string $rule,
719+
string $field,
720+
?string $label = null,
721+
?string $param = null,
722+
?string $value = null,
723+
?string $originalField = null
724+
): string {
711725
$param ??= '';
712726

713727
// Check if custom message has been defined by user
714728
if (isset($this->customErrors[$field][$rule])) {
715729
$message = lang($this->customErrors[$field][$rule]);
730+
} elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
731+
$message = lang($this->customErrors[$originalField][$rule]);
716732
} else {
717733
// Try to grab a localized version of the message...
718734
// lang() will return the rule name back if not found,

tests/system/Validation/ValidationTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,28 @@ public function testRunGroupWithCustomErrorMessage(): void
463463
], $this->validation->getErrors());
464464
}
465465

466+
/**
467+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6245
468+
*/
469+
public function testRunWithCustomErrorsAndAsteriskField(): void
470+
{
471+
$data = [
472+
'foo' => [
473+
['bar' => null],
474+
['bar' => null],
475+
],
476+
];
477+
$this->validation->setRules(
478+
['foo.*.bar' => ['label' => 'foo bar', 'rules' => 'required']],
479+
['foo.*.bar' => ['required' => 'Required']]
480+
);
481+
$this->validation->run($data);
482+
$this->assertSame([
483+
'foo.0.bar' => 'Required',
484+
'foo.1.bar' => 'Required',
485+
], $this->validation->getErrors());
486+
}
487+
466488
/**
467489
* @dataProvider rulesSetupProvider
468490
*

user_guide_src/source/changelogs/v4.2.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ BREAKING
1414

1515
- 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.
1616
- 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.
17+
- The method signature of ``Validation::processRules()`` and ``Validation::getErrorMessage()`` have been changed. Both of these methods add new ``$originalField`` parameter.
1718

1819
Enhancements
1920
************

0 commit comments

Comments
 (0)