Skip to content

Instantly share code, notes, and snippets.

@brayniverse
Last active December 18, 2015 15:22
Show Gist options
  • Save brayniverse/84e9ec5ee0ad3431728a to your computer and use it in GitHub Desktop.
Save brayniverse/84e9ec5ee0ad3431728a to your computer and use it in GitHub Desktop.

This is in response to Matt Stauffer's blog post Form array validation in Laravel 5.2.

At the end Matt explains that you can apply validation rules to a specific array index, he also says that he is "Not sure why you would do this, but, it's possible".

One scenario I have thought of is when you need to require at least one value for a multiple value field, for example a user can have many email addresses.

$this->validate($request->all(), [
  'email_addresses.1' => 'required|email',
  'email_addresses.2' => 'email',
]);

Or if a user can have multiple social websites associated with their account.

$this->validate($request->all(), [
  'social_profiles.twitter.*' => 'regex:^@?(\w){1,15}$',
  'social_profiles.facebook.*' => 'regex:<facebook regex>'
]);

I'm sure there are other examples and definitely other ways of doing the above examples, but hopefully this is food for thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment