close

How to validate array in Laravel?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to validate array in Laravel? in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to validate array in Laravel Error Occurs?

Today I get the following error How to validate array in Laravel? in php.

How To Solve validate array in Laravel Error ?

  1. How To Solve validate array in Laravel Error ?

    To Solve validate array in Laravel Error I have this array as my request data from a HTML+Vue.js data grid/table:

  2. How to validate array in Laravel?

    To Solve validate array in Laravel Error I have this array as my request data from a HTML+Vue.js data grid/table:

Solution 1

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',
]);

Solution 2

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 */ 
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment