Skip to content

Commit

Permalink
Fix: Validation. Error key for field with asterisk
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Pyzhikov <[email protected]>
  • Loading branch information
iRedds committed Feb 15, 2022
1 parent ea2366a commit 7dce1aa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
13 changes: 9 additions & 4 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
$rules = $this->splitRules($rules);
}

$values = dot_array_search($field, $data);
$values = strpos($field, '*') !== false
? array_filter(array_flatten_with_dots($data), static fn ($key) => preg_match(
'/^' . str_replace('\.\*', '\..+', preg_quote($field, '/')) . '$/',
$key
), ARRAY_FILTER_USE_KEY)
: dot_array_search($field, $data);

if ($values === []) {
// We'll process the values right away if an empty array
Expand All @@ -148,10 +153,10 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
continue;
}

if (strpos($field, '*') !== false && is_array($values)) {
if (strpos($field, '*') !== false) {
// Process multiple fields
foreach ($values as $value) {
$this->processRules($field, $setup['label'] ?? $field, $value, $rules, $data);
foreach ($values as $dotField => $value) {
$this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data);
}
} else {
// Process single field
Expand Down
18 changes: 14 additions & 4 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,21 +825,31 @@ public function testRulesForSingleRuleWithAsteriskWillReturnError(): void
],
'name_user' => [
123,
'alpha',
'xyz098',
],
'contacts' => [
'friends' => [
['name' => ''],
['name' => 'John'],
],
],
];

$request = new IncomingRequest($config, new URI(), 'php://input', new UserAgent());

$this->validation->setRules([
'id_user.*' => 'numeric',
'name_user.*' => 'alpha',
'id_user.*' => 'numeric',
'name_user.*' => 'alpha',
'contacts.*.name' => 'required',
]);

$this->validation->withRequest($request->withMethod('post'))->run();
$this->assertSame([
'id_user.*' => 'The id_user.* field must contain only numbers.',
'name_user.*' => 'The name_user.* field may only contain alphabetical characters.',
'id_user.0' => 'The id_user.* field must contain only numbers.',
'name_user.0' => 'The name_user.* field may only contain alphabetical characters.',
'name_user.2' => 'The name_user.* field may only contain alphabetical characters.',
'contacts.friends.0.name' => 'The contacts.*.name field is required.',
], $this->validation->getErrors());
}

Expand Down
20 changes: 20 additions & 0 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,26 @@ If you need to retrieve all error messages for failed fields, you can use the ``

If no errors exist, an empty array will be returned.

When using a wildcard, the error will point to a specific field, replacing the asterisk with the appropriate key/keys.::

// for data
'contacts' => [
'friends' => [
[
'name' => 'Fred Flinstone',
],
[
'name' => '',
],
]
]

//rule
contacts.*.name => 'required'

// error will be
'contacts.friends.1.name' => 'The contacts.*.name field is required.',

Getting a Single Error
======================

Expand Down

0 comments on commit 7dce1aa

Please sign in to comment.