Skip to content

Commit

Permalink
Merge pull request #5938 from kenjis/fix-validation-wildcard
Browse files Browse the repository at this point in the history
fix:  [Validation] Fields with an asterisk throws exception
  • Loading branch information
kenjis authored May 3, 2022
2 parents febc03a + 7e933c8 commit cdf427f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
12 changes: 8 additions & 4 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,16 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
$rules = $this->splitRules($rules);
}

$values = strpos($field, '*') !== false
? array_filter(array_flatten_with_dots($data), static fn ($key) => preg_match(
if (strpos($field, '*') !== false) {
$values = array_filter(array_flatten_with_dots($data), static fn ($key) => preg_match(
'/^' . str_replace('\.\*', '\..+', preg_quote($field, '/')) . '$/',
$key
), ARRAY_FILTER_USE_KEY)
: dot_array_search($field, $data);
), ARRAY_FILTER_USE_KEY);
// if keys not found
$values = $values ?: [$field => null];
} else {
$values = dot_array_search($field, $data);
}

if ($values === []) {
// We'll process the values right away if an empty array
Expand Down
68 changes: 68 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1244,4 +1244,72 @@ public function testPlaceholderReplacementSetMultipleRulesComplexArray()

$this->placeholderReplacementResultDetermination();
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5922
*/
public function testNestedArrayThrowsException(): void
{
$rule = [
'customer_account_number' => [
'label' => 'ACCOUNT NUMBER',
'rules' => 'required|exact_length[5]',
],
'debit_amount' => [
'label' => 'DEBIT AMOUNT',
'rules' => 'required|decimal|is_natural_no_zero',
],
'beneficiaries_accounts.*.account_number' => [
'label' => 'BENEFICIARY ACCOUNT NUMBER',
'rules' => 'exact_length[5]',
],
'beneficiaries_accounts.*.credit_amount' => [
'label' => 'CREDIT AMOUNT',
'rules' => 'required|decimal|is_natural_no_zero',
],
'beneficiaries_accounts.*.purpose' => [
'label' => 'PURPOSE',
'rules' => 'required_without[beneficiaries_accounts.*.account_number]|min_length[3]|max_length[255]',
],
];

$this->validation->setRules($rule);
$data = [
'customer_account_number' => 'A_490',
'debit_amount' => '1500',
'beneficiaries_accounts' => [],
];
$this->validation->run($data);

$this->assertSame([
'beneficiaries_accounts.*.account_number' => 'The BENEFICIARY ACCOUNT NUMBER field must be exactly 5 characters in length.',
'beneficiaries_accounts.*.credit_amount' => 'The CREDIT AMOUNT field is required.',
'beneficiaries_accounts.*.purpose' => 'The PURPOSE field is required when BENEFICIARY ACCOUNT NUMBER is not present.',
], $this->validation->getErrors());

$this->validation->reset();
$this->validation->setRules($rule);
$data = [
'customer_account_number' => 'A_490',
'debit_amount' => '1500',
'beneficiaries_accounts' => [
'account_1' => [
'account_number' => 'A_103',
'credit_amount' => 1000,
'purpose' => 'Personal',
],
'account_2' => [
'account_number' => 'A_258',
'credit_amount' => null,
'purpose' => 'A',
],
],
];
$this->validation->run($data);

$this->assertSame([
'beneficiaries_accounts.account_2.credit_amount' => 'The CREDIT AMOUNT field is required.',
'beneficiaries_accounts.account_2.purpose' => 'The PURPOSE field must be at least 3 characters in length.',
], $this->validation->getErrors());
}
}

0 comments on commit cdf427f

Please sign in to comment.