Add ability to validate arrays explicitly #739
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ok, this is a fork of my other PR (#255) related to Validator.
This PR introduces additional special characters: "[]". Square brackets now are going to be an indicator that certain attribute must be validated as an array of items. And works like that:
As you can see, we have an attribute
tags[]
in the rules set. What it does, is it tells to the Validator thattags
attribute is an array and each of it's items have to be validated against "required" and "alpha" rules.But what if we didn't specify that
tags
is an array?We'll get an exception.
Now, one important not related to array validation with
exists
rule?Also, now you can mix rules:
As you can see, you can use
tags[]
to validate every item in the array against certain rules, while you can also havetags
checking certain rules (exists) over the array as a whole.P.S.: I'm not adding extra commit here, but i'm wondering if
getSize
method should return array length if value is an array. I guess that's for the Issue or another PR.P.P.S: Alternatively to
[]
, maybe used*
. And even more:post.*.title
- validate only title from the array of posts. Requiresarray_get
orgetValue
rewrite.