Igor Simic
5 years ago

How to validate array input in Laravel?

In Laravel 5.2 we have very simple and cool feature to validate each array element

If we have input array like this:

<?php $selectedTags= [ ['id'=1,'name'=>'Tag 1'],['id'=2,'name'=>'Tag 2'] ...]; ?>

And we want to validate each item in $tags array, we should make valdation like this:

$validator = Validator::make($input, [
            'selectedTags.*.id' => 'required|numeric',
            'selectedTags.*.name' => 'required|alpha_spaces', /* alpha spaces ->is custom filter, you can find tutorial here on coditty*/

In your language folder, inside of validation.php you should add these lines:

'custom' => [
        'selectedTags.*.name' => [
            'alpha_spaces' => 'Search tags can contain only alphanumeric characters!',
       'selectedTags.*.id' => [
            'numeric' => 'Search tag id can contain only numbers!',


And your validation is complete!