Skip to content

Commit d6a2327

Browse files
committed
Fix: Validation. Leading asterisk.
1 parent 6b077b7 commit d6a2327

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-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
@@ -1098,6 +1098,12 @@ public function validationArrayDataCaseProvider(): iterable
10981098
['foo' => ['boz']],
10991099
]],
11001100
];
1101+
1102+
yield 'leading-asterisk' => [
1103+
true,
1104+
['*.foo' => 'required'],
1105+
[['foo' => 'bar']],
1106+
];
11011107
}
11021108

11031109
/**
@@ -1324,4 +1330,17 @@ public function testNestedArrayThrowsException(): void
13241330
'beneficiaries_accounts.account_2.purpose' => 'The PURPOSE field must be at least 3 characters in length.',
13251331
], $this->validation->getErrors());
13261332
}
1333+
1334+
public function testRuleWithLeadingAsterisk(): void
1335+
{
1336+
$data = [
1337+
['foo' => 1],
1338+
['foo' => null],
1339+
];
1340+
1341+
$this->validation->setRules(['*.foo' => 'required'], ['1.foo' => ['required' => 'Required {field}']]);
1342+
1343+
$this->assertFalse($this->validation->run($data));
1344+
$this->assertSame('Required *.foo', $this->validation->getError('*.foo'));
1345+
}
13271346
}

0 commit comments

Comments
 (0)