Closed luispastendev closed 3 years ago
@luispastendev it's wrong record, can you public your code?
'bar.*.foo' => [
'rules' => 'required'
]
@WinterSilence you can test with this code
$validation = Services::Validation();
if(!$validation->setRules([
'bar.*.foo' => [
'rules' => 'required'
]
])->run([
"bar" => [
[
'foo' => 'baz'
],
[
'foo' => ''
]
]
])){
return $this->fail($validation->getErrors());
}
return $this->respond('ok!');
I was reviewing the code but it seems that it is only supported for selectors that end with *, but the documentation also mentions the possibility of doing something like the example above
If it is not a bug, I suppose that you should delete that text from the documentation
link: https://codeigniter4.github.io/userguide/libraries/validation.html#validating-keys-that-are-arrays
$validation->setRules(['bar.*.foo' => 'required']);
not same to
->setRules(['bar.*.foo' => ['rules' => 'required']])
@WinterSilence the same result
@luispastendev CI 4.1.1:
$result = \Config\Services::validation()
->setRules(['bar.*.foo' => 'required'])
->run([
"bar" => [
['foo' => 'baz'],
['foo' => '']
]
]);
$result2 = \Config\Services::validation()
->setRules(['bar.*.foo' => 'required'])
->run([
"bar" => [
['foo' => 'baz'],
['foo' => 'bar']
]
]);
$result3 = \Config\Services::validation()
->setRules(['bar.*.foo' => 'required'])
->run([
"bar" => [
['foo2' => 'baz'],
['foo2' => 'bar']
]
]);
var_export([$result, $result2, $result3]); // true, true, false
@WinterSilence I think the required rule should fail if an empty value is sent. taking your examples if you try the following fails and under what you explain it should return true
$result4 = \Config\Services::validation()
->setRules(['bar.*.foo' => 'required'])
->run([
"bar" => [
['foo' => ''],
['foo' => '']
]
]);
var_dump($result4); // currently returns false
doc:
@luispastendev all right, i fix my comment later
@luispastendev but method really have other bug:
$result = \Config\Services::validation()
->setRules(['bar.*' => 'required'])
->run(["bar" => []]); // return true
@WinterSilence right! it only works if it detects something empty ['']
that seems to be fixed with:
by the following:
if (is_array($value) && end($fieldNameToken) === '*' && ! empty($value))
but the other problem I do not know what others think at least reading the code I do not see an intention to support that use case
hi there!
validation with more than 1 level only validates the first match.
Example:
the above validation should fails, however it passes. https://codeigniter4.github.io/userguide/libraries/validation.html#validating-keys-that-are-arrays