Skip to content

Commit 2a4b614

Browse files
committed
fix: add a new param in processRules and getErrorMessage function to judge if validation fields have asterisk
1 parent 42e5b69 commit 2a4b614

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

system/Validation/Validation.php

Lines changed: 23 additions & 7 deletions
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,

0 commit comments

Comments
 (0)