Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [Validation] Fields with an asterisk throws exception #5938

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}