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

Array validation does not seem to be working as expected. #13212

Closed
godjen99 opened this issue Apr 19, 2016 · 4 comments
Closed

Array validation does not seem to be working as expected. #13212

godjen99 opened this issue Apr 19, 2016 · 4 comments

Comments

@godjen99
Copy link

When trying the following code, I was expecting a false when checking if it passed, however that wasn't the case. All passed.

$data = [
    'companies' => ['dddd']
];

$validator = \Validator::make($data, [
    'companies.*.name' => 'required|max:255',
]);

dd($validator->passes());

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!

@azorgh
Copy link

azorgh commented Apr 19, 2016

Hi,

I think your data array must be like :

$data = [
    'companies' => [
        ['dddd'],
        ['name' => 'My Company']
    ]
];

@godjen99
Copy link
Author

godjen99 commented Apr 19, 2016

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:

['companies' => [
   ['name' => 'Company 1']
   ['name' => 'Company 2']
]];

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:

$data = [
    'companies' => ['dddd']
];

$validator = \Validator::make($data, [
    'companies' => 'required|array',
    'companies.*' => 'required|array',
    'companies.*.name' => 'required|max:255',
]);

dd($validator->passes()); // now comes back as false, as expected.

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.

@themsaid
Copy link
Member

@GrahamCampbell this one is fixed too :)

@GrahamCampbell
Copy link
Member

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants