Journal & Notebook

Laravel Example: Tag Validation

Eric Barnes provides a great way to validate information entered in form input named arrays on his blog (i.e. an input field named something like inputItem[1]).

We want more

However, I wanted to be able to validate a requirement for a field that is named with the [] designation so that it is converted to an array when posted.

A practical example

In this case, I'm using the Select2 plugin and I also want the entered values to come into my backend as an array. You should first name your select field as <select multiple id="tags[]"> or if you have the laravel Form library {!! Form::select('tags[]', $tags, null, ['multiple']) !!}

When you write your validation rules, note that you will access the tags from the request using the name without brackets (i.e. tags instead of tags[]).

Here's what a required validation rule looks like for this particular example: 'tags' => 'required|array'