Skip to content

Conversation

@KaneCohen
Copy link
Contributor

Currently, there's no way to validate contents of input arrays. This pull request tries to fix that.

Example:

<input name="tags[]" value="foo"/>
<input name="tags[]" value="bar" />
<input name="tags[]" value="baz" />

will send to PHP following array:

array('tags' => array('foo', 'bar', 'baz'));

Right now there's no way to check whole array for a certain rules, like alpha/numeric etc. However, with this patch you can validate tags array like so:

$val = Validator::make(array('tags' => array('foo', 'bar', 'baz')), array('tags' => 'required|alpha'));

Validator will determine that tags key refers to the array and will proceed to validate each element according to set rules required|alpha;

This patch also allows to use dot notation to validate inner arrays of other arrays:

<input name="tags[new][]" value="foo"/>
<input name="tags[new][]" value="bar" />
<input name="tags[new][]" value="baz" />
$input = array('tags' => array(
    'new' => array('foo', 'bar', 'baz'),
    'old' => array()
));

$val = Validator::make($input, array('tags.new' => 'required|alpha'));

In this case Validator will validate new array that is inside of the tags array.

P.S.: Some of the test were omitted because of a similarities with other tests.

@franzliedke
Copy link
Contributor

I believe you can validate nested fields with the array dot syntax: tags.new.0 would let you access the first element etc.

@JoostK
Copy link
Contributor

JoostK commented Feb 5, 2013

This PR allows for validating tags.new.%d, so for every element inside an array.

@KaneCohen
Copy link
Contributor Author

@franzliedke, i don't believe so since getValue() method retrieves associated element directly by attribute name (key in data/files array):

if (array_key_exists($attribute, $this->data))
{
    return $this->data[$attribute];
}
elseif (array_key_exists($attribute, $this->files))
{
    return $this->files[$attribute];
}

But even if that was a case, it's still only one element out of the array that might have an unknown size. Sure, you might generate rules based on the length of the input array, but that seems like an unnecessary hacking.

@franzliedke
Copy link
Contributor

Ah, okay. Thanks for clarifying and sorry for not double-checking, guys.

Fix validateExists to work with arrays as a whole

Swap if/else in validateSame/Different
@scbenjamin
Copy link

+1 on this.

@Anahkiasen
Copy link
Contributor

+1

@scbenjamin
Copy link

@KaneCohen Using the above, how are custom error messages handled? The same way?

@KaneCohen
Copy link
Contributor Author

@scbenjamin, yes, the same way.

Taking example above:

$input = array('tags' => array(
    'new' => array('foo', 'bar1', 'baz'),
));
$val = Validator::make($input, array('tags.new' => 'required|alpha'));

Validation will fail because bar1 contains non alpha character.

validation.php file for language:

'custom' => array(
    'tags.new.alpha' => 'Tags may contain only alphabetical characters.'
),

@billmn
Copy link
Contributor

billmn commented Mar 26, 2013

👍

@taylorotwell
Copy link
Member

For the exists validation rule, wouldn't this run a lot of queries?

@KaneCohen
Copy link
Contributor Author

@taylorotwell, hmm, yeah. It seems like that thing (validate method to be exact) needs a rewrite so that when Validator hits DB-related rule it'll use "whereIn" to check for existence of the input and won't go through every item.

@KaneCohen KaneCohen mentioned this pull request May 9, 2013
joelharkes added a commit to joelharkes/framework_old that referenced this pull request Mar 7, 2019
Fix File uploads because blob size was not big enough; Fixes laravel#253
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

Successfully merging this pull request may close these issues.

7 participants