带有通配符的Laravel验证器

Alh*_*nIQ 6 php laravel laravel-5 laravel-5.1

我想制作一个laravel验证器来验证数组内部未命名数组(0,1,2,3)内的字段

所以我的阵列就像

array [ //the form data
  "items" => array:2 [ //the main array i want to validate
    0 => array:2 [ // the inner array that i want to validate its data
      "id" => "1"
      "quantity" => "1000"
     ]
    1 => array:2 [
     "id" => "1"
     "quantity" => "1000"
     ]
  // other fields of the form,
  ]

]
Run Code Online (Sandbox Code Playgroud)

所以我想要的是类似的东西

  $validator = Validator::make($request->all(), [
     'items.*.id' => 'required' //notice the star *
  ]);
Run Code Online (Sandbox Code Playgroud)

and*_*ber 6

Laravel 5.2

现在支持问题中的语法

http://laravel.com/docs/master/validation#validating-arrays

Laravel 5.1

首先使用所有其他规则创建验证器.使用array项目规则

$validator = Validator::make($request->all(), [
    'items' => 'array',
    // your other rules here
]);
Run Code Online (Sandbox Code Playgroud)

然后使用Validator each方法将一组规则应用于items数组中的每个项目.

$validator->each('items', [
    'id'       => 'required',
    'quantity' => 'min:0', 
]);
Run Code Online (Sandbox Code Playgroud)

这将自动为您设置这些规则......

items.*.id       => required
items.*.quantity => min:0
Run Code Online (Sandbox Code Playgroud)

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Validation/Validator.php#L261