Skip to content

Commit bc688d9

Browse files
authored
Merge pull request #6243 from iRedds/fix/validation-leading-asterisk
Fix: Validation of fields with a leading asterisk.
2 parents ba7d36c + 770d5a1 commit bc688d9

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

system/Validation/Validation.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
147147

148148
if (strpos($field, '*') !== false) {
149149
$values = array_filter(array_flatten_with_dots($data), static fn ($key) => preg_match(
150-
'/^' . str_replace('\.\*', '\..+', preg_quote($field, '/')) . '$/',
150+
'/^'
151+
. str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/'))
152+
. '$/',
151153
$key
152154
), ARRAY_FILTER_USE_KEY);
153155
// if keys not found
@@ -657,7 +659,7 @@ public function getError(?string $field = null): string
657659
}
658660

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

tests/system/Validation/ValidationTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,12 @@ public function validationArrayDataCaseProvider(): iterable
11311131
['foo' => ['boz']],
11321132
]],
11331133
];
1134+
1135+
yield 'leading-asterisk' => [
1136+
true,
1137+
['*.foo' => 'required'],
1138+
[['foo' => 'bar']],
1139+
];
11341140
}
11351141

11361142
/**
@@ -1357,4 +1363,17 @@ public function testNestedArrayThrowsException(): void
13571363
'beneficiaries_accounts.account_2.purpose' => 'The PURPOSE field must be at least 3 characters in length.',
13581364
], $this->validation->getErrors());
13591365
}
1366+
1367+
public function testRuleWithLeadingAsterisk(): void
1368+
{
1369+
$data = [
1370+
['foo' => 1],
1371+
['foo' => null],
1372+
];
1373+
1374+
$this->validation->setRules(['*.foo' => 'required'], ['1.foo' => ['required' => 'Required {field}']]);
1375+
1376+
$this->assertFalse($this->validation->run($data));
1377+
$this->assertSame('Required *.foo', $this->validation->getError('*.foo'));
1378+
}
13601379
}

user_guide_src/source/changelogs/v4.2.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Changes
2525
*******
2626

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

0 commit comments

Comments
 (0)