Skip to content

Commit 09e267d

Browse files
committed
add support for display of error message using wildcard (*)
1 parent 770351f commit 09e267d

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

system/Helpers/form_helper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,17 +741,20 @@ function validation_show_error(string $field, string $template = 'single'): stri
741741
$config = config('Validation');
742742
$view = Services::renderer();
743743

744-
$errors = validation_errors();
744+
$errors = array_filter(validation_errors(), static fn ($key) => preg_match(
745+
'/^' . str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/')) . '$/',
746+
$key
747+
), ARRAY_FILTER_USE_KEY);
745748

746-
if (! array_key_exists($field, $errors)) {
749+
if ($errors === []) {
747750
return '';
748751
}
749752

750753
if (! array_key_exists($template, $config->templates)) {
751754
throw ValidationException::forInvalidTemplate($template);
752755
}
753756

754-
return $view->setVar('error', $errors[$field])
757+
return $view->setVar('error', implode("\n", $errors))
755758
->render($config->templates[$template]);
756759
}
757760
}

tests/system/Helpers/FormHelperTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,16 @@ public function testValidationShowError()
996996
$this->assertSame('<span class="help-block">The ID field is required.</span>' . "\n", $html);
997997
}
998998

999+
public function testValidationShowErrorForWildcards()
1000+
{
1001+
$validation = Services::validation();
1002+
$validation->setRule('user.0.name', 'Name', 'required')->run([]);
1003+
1004+
$html = validation_show_error('user.*.name');
1005+
1006+
$this->assertSame('<span class="help-block">The Name field is required.</span>' . "\n", $html);
1007+
}
1008+
9991009
public function testFormParseFormAttributesTrue()
10001010
{
10011011
$expected = 'readonly ';

0 commit comments

Comments
 (0)