How to validate array in Laravel?
Asked Answered
T

7

205

I try to validate array POST in Laravel:

$validator = Validator::make($request->all(), [
    "name.*" => 'required|distinct|min:3',
    "amount.*" => 'required|integer|min:1',
    "description.*" => "required|string"
             
]);

I send empty POST and get this if ($validator->fails()) {} as False. It means that validation is true, but it is not.

How to validate array in Laravel? When I submit a form with input name="name[]"

Thay answered 15/2, 2017 at 19:15 Comment(0)
L
458

Asterisk symbol (*) is used to check values in the array, not the array itself.

$validator = Validator::make($request->all(), [
    "names"    => "required|array|min:3",
    "names.*"  => "required|string|distinct|min:3",
]);

In the example above:

  • "names" must be an array with at least 3 elements,
  • values in the "names" array must be distinct (unique) strings, at least 3 characters long.

EDIT: Since Laravel 5.5 you can call validate() method directly on Request object like so:

$data = $request->validate([
    "name"    => "required|array|min:3",
    "name.*"  => "required|string|distinct|min:3",
]);
Latchstring answered 15/2, 2017 at 19:45 Comment(6)
remember to place it in a try catch if you are using $request->validate([...]). An exception will be raised if the data is fails the validation.Wit
how to get the error message of a specific field? like I have 2 fields of name, and then it's the second field that has only the error, how can I attain it?Turpitude
the required on the 'name.*' isn't needed as it only validates it if it existsAlbumin
This is brilliant. I wonder why there are no examples of this in the official documentation of array validation.Cymbre
How to display msg then for example $validator = Validator::make($request->all(), [ "names" => "required|array|min:3", "names.*" => "required|string|distinct|min:3", ],[ name.required=>"You left this name" ]); IS it correct ? If it is then its not working for meRetsina
This is not useful solution. I have 2 name elements as array when I submit the form after applying given solution, error message is showing like 'The name.1 field is required.', 'The name.2 field is required.'. Is this user friendly message??Lawry
Q
86

I have this array as my request data from a HTML+Vue.js data grid/table:

[0] => Array
    (
        [item_id] => 1
        [item_no] => 3123
        [size] => 3e
    )
[1] => Array
    (
        [item_id] => 2
        [item_no] => 7688
        [size] => 5b
    )

And use this to validate which works properly:

$this->validate($request, [
    '*.item_id' => 'required|integer',
    '*.item_no' => 'required|integer',
    '*.size'    => 'required|max:191',
]);
Quintet answered 14/10, 2017 at 7:17 Comment(1)
Nice answer but I have arrays like so and my forms has radio buttons with the same name, when I submit I get back validation errors because of radio buttons sharing names.Cerography
O
36

The recommended way to write validation and authorization logic is to put that logic in separate request classes. This way your controller code will remain clean.

You can create a request class by executing php artisan make:request SomeRequest.

In each request class's rules() method define your validation rules:

//SomeRequest.php
public function rules()
{
   return [
    "name"    => [
          'required',
          'array', // input must be an array
          'min:3'  // there must be three members in the array
    ],
    "name.*"  => [
          'required',
          'string',   // input must be of type string
          'distinct', // members of the array must be unique
          'min:3'     // each string must have min 3 chars
    ]
  ];
}

In your controller write your route function like this:

// SomeController.php
public function store(SomeRequest $request) 
{
  // Request is already validated before reaching this point.
  // Your controller logic goes here.
}

public function update(SomeRequest $request)
{
  // It isn't uncommon for the same validation to be required
  // in multiple places in the same controller. A request class
  // can be beneficial in this way.
}

Each request class comes with pre- and post-validation hooks/methods which can be customized based on business logic and special cases in order to modify the normal behavior of request class.

You may create parent request classes for similar types of requests (e.g. web and api) requests and then encapsulate some common request logic in these parent classes.

Octodecillion answered 31/1, 2019 at 6:43 Comment(0)
V
26

Little bit more complex data, mix of @Laran's and @Nisal Gunawardana's answers

[ 
   {  
       "foodItemsList":[
    {
       "id":7,
       "price":240,
       "quantity":1
                },
               { 
                "id":8,
                "quantity":1
               }],
        "price":340,
        "customer_id":1
   },
   {   
      "foodItemsList":[
    {
       "id":7,
       "quantity":1
    },
    { 
        "id":8,
        "quantity":1
    }],
    "customer_id":2
   }
]

The validation rule will be

 return [
            '*.customer_id' => 'required|numeric|exists:customers,id',
            '*.foodItemsList.*.id' => 'required|exists:food_items,id',
            '*.foodItemsList.*.quantity' => 'required|numeric',
        ];
Varve answered 10/10, 2019 at 13:15 Comment(0)
S
3

You have to loop over the input array and add rules for each input as described here: Loop Over Rules

Here is a some code for ya:

$input = Request::all();
$rules = [];

foreach($input['name'] as $key => $val)
{
    $rules['name.'.$key] = 'required|distinct|min:3';
}

$rules['amount'] = 'required|integer|min:1';
$rules['description'] = 'required|string';

$validator = Validator::make($input, $rules);

//Now check validation:
if ($validator->fails()) 
{ 
  /* do something */ 
}
Sava answered 15/2, 2017 at 19:47 Comment(1)
There is no need to do that - laravel.com/docs/5.4/validation#validating-arraysLatchstring
P
1

The below code working for me on array coming from ajax call .

  $form = $request->input('form');
  $rules = array(
            'facebook_account' => 'url',
            'youtube_account' => 'url',
            'twitter_account' => 'url',
            'instagram_account' => 'url',
            'snapchat_account' => 'url',
            'website' => 'url',
        );
        $validation = Validator::make($form, $rules);

        if ($validation->fails()) {
            return Response::make(['error' => $validation->errors()], 400);
        }
Proletariat answered 22/11, 2020 at 11:34 Comment(0)
M
1

In my Case it works fine

$validator = Validator::make($request->all(), [
    "names" => "required|array|min:3",
    "*.names"=> "required|string|distinct|min:3",
]);
Marston answered 13/5, 2022 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.