-
Notifications
You must be signed in to change notification settings - Fork 10.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
Array validation does not seem to be working as expected. #13212
Comments
Hi, I think your data array must be like : $data = [
'companies' => [
['dddd'],
['name' => 'My Company']
]
]; |
Hello pallmallshow, You're correct, that ends up failing, as expected. However my intent for the API endpoint I was creating was to force the need for a companies key and many "fields" of data (to create many companies at once). so I'm expecting data like:
And missing the fields should be a "break" or a failed validation. So, when I made the rule ('companies.*.name'), I was thinking like this: "Require a companies key and any sub keys must have the "name" field". Anyway, after talking with a co-worker and playing with the code some, we found a workaround, but doesn't seem to fit how I would have expected it to work. So the way I fixed it was like this:
I do wonder the point of the validation if I have to explicitly add the other rules. Is there any reason the validation shouldn't work like I thought it should? Which would be the fact I have a "required" on an array subset with that subset missing should result in failed validation. |
@GrahamCampbell this one is fixed too :) |
Thanks. |
When trying the following code, I was expecting a false when checking if it passed, however that wasn't the case. All passed.
Digging into the code, In the Illuminate/Validation/Validator.php on line 306, it's looking for "companies.*.name" in "companies.0", but doesn't find it (returns false), then looks to see if the regular expression "companies.[^.]+.name" matches "companies.0", and it doesn't as well, so the rules don't get merged into the ruleset and allow for passing.
Is this intended? I'm not sure the fix, but I've been thinking on how to approach it, but don't want to try doing my first pull request on something thats intended ;)
Thanks for any help!
The text was updated successfully, but these errors were encountered: