From d6a2327f5a1b70c9197521b5ea6bc60d49a26e6f Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Fri, 8 Jul 2022 15:31:54 +0800 Subject: [PATCH 1/2] Fix: Validation. Leading asterisk. --- system/Validation/Validation.php | 6 ++++-- tests/system/Validation/ValidationTest.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index eb91114ca218..61fbb60e7bdd 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -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 @@ -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); diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index c43b7d997d88..530205243976 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -1098,6 +1098,12 @@ public function validationArrayDataCaseProvider(): iterable ['foo' => ['boz']], ]], ]; + + yield 'leading-asterisk' => [ + true, + ['*.foo' => 'required'], + [['foo' => 'bar']], + ]; } /** @@ -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}']]); + + $this->assertFalse($this->validation->run($data)); + $this->assertSame('Required *.foo', $this->validation->getError('*.foo')); + } } From 770d5a1206973e97a104317540ac6ca425c8876c Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Fri, 8 Jul 2022 15:36:54 +0800 Subject: [PATCH 2/2] Fix: Validation. Leading asterisk. --- user_guide_src/source/changelogs/v4.2.2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelogs/v4.2.2.rst b/user_guide_src/source/changelogs/v4.2.2.rst index 45fbeb205d54..eb457750e7c5 100644 --- a/user_guide_src/source/changelogs/v4.2.2.rst +++ b/user_guide_src/source/changelogs/v4.2.2.rst @@ -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.