[Draft] fix: Validation passes if key does not exist when using asterisk.#8079
[Draft] fix: Validation passes if key does not exist when using asterisk.#8079ping-yee wants to merge 1 commit intocodeigniter4:developfrom
Conversation
|
@kenjis Do you have any idea about this problem? |
|
The following tests show the current behaviors for single field. public function testRunRequiredSingleFieldEmptyData(): void
{
$config = new ValidationConfig();
$validation = new Validation($config, Services::renderer());
$validation->setRules([
'name' => 'required',
]);
$data = [];
$result = $validation->run($data);
$this->assertFalse($result);
$errors = $validation->getErrors();
$this->assertSame($errors, ['name' => 'The name field is required.']);
}
public function testRunAlphaSingleFieldEmptyData(): void
{
$config = new ValidationConfig();
$validation = new Validation($config, Services::renderer());
$validation->setRules([
'name' => 'alpha',
]);
$data = [];
$result = $validation->run($data);
$this->assertFalse($result);
$errors = $validation->getErrors();
$this->assertSame(
$errors,
['name' => 'The name field may only contain alphabetical characters.']
);
} |
|
$data = [
'contacts' => [
'friends' => [
['name' => 'Fred Flinstone', 'age' => 20],
['age' => 21], // 'name' key does not exist
]
]
];
$data = [
'contacts' => [
'friends' => [
['name' => 'Fred Flinstone', 'age' => 20],
[
'name' => null, // add 'name' key
'age' => 21,
],
],
],
]; |
|
There are some problem I should figure out first:
|
|
https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#setting-rules-for-array-data I sent a PR #8123 that is related to this topic. |
|
https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#setting-rules-for-array-data // Fred Flintsone & Wilma
$validation->setRules([
'contacts.*.name' => 'required|max_length[60]',
]);The Validation using wildcards ( |
I am agree with this, the before check is neccessary. |
Yes, I also agree with this. The caption of user guide make me so confused. 😖 public function index(): string
{
// Extend the user guide case and add one more layer.
$requestData = [
'contacts' => [
'name' => 'Joe Smith',
'just' => [
'friends' => [
[
'name' => 'Fred Flinstone',
],
[
'name' => 'Wilma',
],
]
]
]
];
$this->validator = \Config\Services::validation();
$this->validator->setRules([
'contacts.*.name' => 'required|max_length[60]',
]);
dd($this->validator->run($requestData), $this->validator->getErrors());
}Output |
public function index(): string
{
// Extend the user guide case and add one more layer.
$requestData = [
'contacts' => [
'name' => 'Joe Smith',
'just' => [
'friends' => [
[
'name' => 'Fred Flinstone',
],
[
'name' => 'Wilma',
],
],
],
],
];
$this->validator = \Config\Services::validation();
$this->validator->setRules([
'contacts.*.name' => 'required|max_length[60]',
]);
dd(
$this->validator->run($requestData),
$this->validator->getErrors(),
$this->validator->getValidated()
);
} |
public function index(): string
{
// Extend the user guide case and add one more layer.
$requestData = [
'contacts' => [
'name' => 'Joe Smith',
'just' => [
'friends' => [
[
'name' => 'Fred Flinstone',
],
[
'name' => 'Wilma',
],
],
],
],
];
$this->validator = \Config\Services::validation();
$this->validator->setRules([
'contacts.*.name' => 'required|max_length[1]',
]);
dd(
$this->validator->run($requestData),
$this->validator->getErrors(),
$this->validator->getValidated()
);
} |
That seems to be a bug. |
|
This looks good. public function index(): string
{
$requestData = [
'contacts' => [
'name' => 'Joe Smith',
'friends' => [
[
'name' => 'Fred Flinstone',
],
[
'name' => 'Wilma',
],
],
],
];
$this->validator = \Config\Services::validation();
$this->validator->setRules([
'contacts.friends.*.name' => 'required|max_length[60]',
]);
dd(
$this->validator->run($requestData),
$this->validator->getErrors(),
$this->validator->getValidated()
);
} |
|
The second example in https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#setting-rules-for-array-data $this->validator->setRules([
'contacts.friends.name' => 'required|max_length[60]',
]); |
f1dac23 to
fbe9df3
Compare
I think so.. and it should be like this: public function index(): string
{
$requestData = [
'contacts' => [
'name' => 'Joe Smith',
'friends' => [
'name' => 'Fred Flinstone',
],
],
];
$this->validator = \Config\Services::validation();
$this->validator->setRules([
'contacts.friends.name' => 'required|max_length[60]',
]);
dd(
$this->validator->run($requestData),
$this->validator->getErrors(),
$this->validator->getValidated()
);
} |
|
I created issue #8128 |
|
Is there any thing that I need to do for this PR? @kenjis |
|
Okay and thanks! |
|
Your comment #8079 (comment) was very helpful! |
|
I send PR #8131 to add method to check array key with dot array syntax. |
Description
See #8006
But This PR still is draft, I need to discussion and find out how to fix this problem.
Checklist: