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 of fields with a leading asterisk. #6243

Merged
merged 2 commits into from
Jul 9, 2022
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
6 changes: 4 additions & 2 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup

if (strpos($field, '*') !== false) {
$values = array_filter(array_flatten_with_dots($data), static fn ($key) => preg_match(
'/^' . str_replace('\.\*', '\..+', preg_quote($field, '/')) . '$/',
'/^'
. str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/'))
. '$/',
$key
), ARRAY_FILTER_USE_KEY);
// if keys not found
Expand Down Expand Up @@ -657,7 +659,7 @@ public function getError(?string $field = null): string
}

$errors = array_filter($this->getErrors(), static fn ($key) => preg_match(
'/^' . str_replace('\.\*', '\..+', preg_quote($field, '/')) . '$/',
'/^' . str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/')) . '$/',
$key
), ARRAY_FILTER_USE_KEY);

Expand Down
19 changes: 19 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,12 @@ public function validationArrayDataCaseProvider(): iterable
['foo' => ['boz']],
]],
];

yield 'leading-asterisk' => [
true,
['*.foo' => 'required'],
[['foo' => 'bar']],
];
}

/**
Expand Down Expand Up @@ -1324,4 +1330,17 @@ public function testNestedArrayThrowsException(): void
'beneficiaries_accounts.account_2.purpose' => 'The PURPOSE field must be at least 3 characters in length.',
], $this->validation->getErrors());
}

public function testRuleWithLeadingAsterisk(): void
{
$data = [
['foo' => 1],
['foo' => null],
];

$this->validation->setRules(['*.foo' => 'required'], ['1.foo' => ['required' => 'Required {field}']]);
Copy link
Member

@kenjis kenjis Jul 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related with this bug, but ['1.foo' => ['required' => 'Required {field}']] seems tricky .
I would like to write ['*.foo' => ['required' => 'Required {field}']].

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know, it turned out that the option you suggested does not work. The default error text is returned.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error text will be defined for the *.foo field, and the 1.foo field is passed.


$this->assertFalse($this->validation->run($data));
$this->assertSame('Required *.foo', $this->validation->getError('*.foo'));
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Changes
*******

- Fixed: ``BaseBuilder::increment()`` and ``BaseBuilder::decrement()`` do not reset the ``BaseBuilder`` state after a query.
- Fixed: Validation of fields with a leading asterisk (wildcard).
- Now ``CLIRequest::isCLI()`` always returns true.
- Now ``IncommingRequest::isCLI()`` always returns false.

Expand Down