How to validate array in Laravel?

ぃ、小莉子 提交于 2019-11-26 21:47:13
Laran

Asterisk symbol (*) means that you want to check VALUES in the array, not the actual array.

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

In the example above:

  • "Name" must be an array with at least 3 elements.
  • Values in the "name" 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",
]);
Nisal Gunawardana

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

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 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

public function someFunction(SomeRequest $request) 
{
  //request is already validated before reaching this point
  //your controller logic goes here
}

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 type of requests for eg web and api requests and then encapsulate some common request logic in these parent classes.

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 */ 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!