-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Bug: [Validation] required_without
will not work if the field is specified with asterisk
#5942
Comments
required_without
will not work if the field is specified with a wildcard (asterisk)required_without
will not work if the field is specified with dot-notation
required_without
will not work if the field is specified with dot-notationrequired_without
will not work if the field is specified with asterisk
I think the description of
Is the description different from the value of
|
@kenjis What kind of design should I use? |
Yes. The explanation is the same as the following comment, but it seems different from the Example blow: CodeIgniter4/system/Validation/Rules.php Lines 274 to 279 in ac2ad22
|
I think this explanation and description of official document of
|
CodeIgniter4/system/Validation/Rules.php Lines 302 to 304 in ac2ad22
As the conditional shows, it judge whether the param field of For example: public function testRequireWithoutWithWildCard()
{
$data = [
'a' => [
['b' => 1, 'c' => 2],
['c' => ''],
],
];
$this->validation->setRules([
'a.*.c' => 'required_without[a.*.b]',
])->run($data);
$this->assertSame(
'The a.*.c field is required when a.*.b is not present.',
$this->validation->getError('a.1.c')
);
} In this test case, $data = [
'a' => [
['b' => '', 'c' => 2],
['c' => ''],
],
]; |
@ping-yee Do you mean this is not a bug? I thought that |
Yes. I think this is not a bug, it just need to change the command out and the description of official document.
Yes. It should be that. public function testRequireWithoutWithWildCard()
{
$data = [
'a' => [
['b' => '', 'c' => 2],
['c' => ''],
],
];
$this->validation->setRules([
'a.*.c' => 'required_without[a.*.b]',
])->run($data);
$this->assertSame(
'The a.*.c field is required when a.*.b is not present.',
$this->validation->getError('a.1.c')
);
} When |
I thought the checking should be done for each array ( |
I read through this whole thread and thought to myself: "has our validation system gotten too abstruse?" I can't think of an example where I would use this, but also it seems so difficult to understand I think if I did have such a case I would break it into smaller validations. |
To be frank, I am not sure of the correct specifications. |
Sorry, I think What I express is too abstruse 😢, I think I figure out and find a bug last night. And I want to check the process logic and design with you all.
public function index()
{
$data = [
'a' => [
['b' => 1, 'c' => ''],
['b' => '', 'c' => 2],
['c' => ''],
],
];
$this->validation->setRules([
'a.*.c' => 'required_without[a.*.b]',
])->run($data);
dd($this->validation->getError('a.2.c'));
} To begin with, we can see these code in last CodeIgniter4/system/Validation/Validation.php Lines 168 to 176 in ac2ad22
In this case, it input the one by one
In CodeIgniter4/system/Validation/Validation.php Lines 313 to 315 in ac2ad22
Here we are If it isn't empty string, means there is data in key of setRule() field this time then return If it is empty string, then we keep go on. 😓 CodeIgniter4/system/Validation/Rules.php Lines 283 to 308 in ac2ad22
CodeIgniter4/system/Validation/Rules.php Lines 214 to 229 in ac2ad22
In line
array(2) {
[0]=>
string(0) ""
[1]=>
string(0) ""
} The condition judge whether the array return by CodeIgniter4/system/Validation/Validation.php Lines 340 to 347 in ac2ad22
And This is all of process logic and there is a design problem will show at next comment. |
I find a bug is If there are more than two fields in data set, So I fixed this issue in PR, Add these to check whether if (strpos($field, '.') !== false && is_array(dot_array_search($field, $data))) {
foreach (dot_array_search($field, $data) as $value) {
if (empty($value)) {
return false;
}
}
} And this change can all pass in
But this design is when any of public function index()
{
$data = [
'a' => [
['b' => 1, 'c' => ''],
['b' => '', 'c' => 2],
['c' => ''],
],
];
$this->validation->setRules([
'a.*.c' => 'required_without[a.*.b]',
])->run($data);
// This will error occur.
dd($this->validation->getError('a.0.c'));
// This will not error occur.
dd($this->validation->getError('a.1.c'));
// This will error occur.
dd($this->validation->getError('a.2.c'));
} In this case, becase And this is why After figure out what the author of this class design, I think this is reasonable for this design becasue it conform the word
I think this is make sense. |
@kenjis Are these thing and use case reasonable? or anything I miss. |
Sorry, I don't get what you say. It is too complicated.
It seems |
In your PR: But your explanation above is: error, pass, pass. |
In my opinion, So the results should be: pass, pass, error. |
CodeIgniter4/system/Validation/Rules.php Lines 214 to 229 in ac2ad22
at last judge, it seems the empty string also means doesn't present the data.
Sorry for the mistake comment out, I have been modified. |
It doesn't run with a pair of field like public function index()
{
$data = [
'a' => [
['b' => 1, 'c' => ''],
['b' => '', 'c' => 2],
['c' => ''],
],
];
$this->validation->setRules([
'a.*.c' => 'required_without[a.*.b]',
])->run($data);
// This will error occur.
dd($this->validation->getError('a.0.c'));
// This will not error occur.
dd($this->validation->getError('a.1.c'));
// This will error occur.
dd($this->validation->getError('a.2.c'));
} So this is why |
Thank you for your explanation! But I don't know it is correct as the specification. In the case 3 in #5922, my opinion seems appropriate. |
I got this logic after reading the
I agree with this, this use case is more appropriate using your opinion because it shouldn't have dependency between accounts. |
It seems to what I think we need:
Because now on the @kenjis what do you think about this? |
I don't believe the original developer design. I think the original developer did not consider about fields with |
Yes, and this issue is the bug report. |
Ok, I got it. |
Closed by #6589 |
See #5922 (comment)
The following test fails. The validation passes.
The text was updated successfully, but these errors were encountered: