Skip to content

Commit

Permalink
Fix: Validation. Leading asterisk.
Browse files Browse the repository at this point in the history
  • Loading branch information
iRedds committed Jul 8, 2022
1 parent 6b077b7 commit d6a2327
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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}']]);

$this->assertFalse($this->validation->run($data));
$this->assertSame('Required *.foo', $this->validation->getError('*.foo'));
}
}

0 comments on commit d6a2327

Please sign in to comment.