我尝试在Laravel中验证数组POST:
$validator = Validator::make($request->all(), [ "name.*" => 'required|distinct|min:3', "amount.*" => 'required|integer|min:1', "description.*" => "required|string" ]);
我发送空的POST,并将其if ($validator->fails()) {}作为False。这意味着验证是正确的,但事实并非如此。
if ($validator->fails()) {}
False
如何在Laravel中验证数组?当我提交表格时input name="name[]"
input name="name[]"
星号(*)表示您要检查数组中的值,而不是实际数组。
$validator = Validator::make($request->all(), [ "name" => "required|array|min:3", "name.*" => "required|string|distinct|min:3", ]);
在上面的示例中:
编辑: 由于Laravel 5.5,您可以像这样直接在Request对象上调用validate()方法:
$data = $request->validate([ "name" => "required|array|min:3", "name.*" => "required|string|distinct|min:3", ]);