如何在 Laravel 验证数组?

我尝试在 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。这意味着验证是真实的,但它不是。

如何在 Laravel 验证数组

310077 次浏览

星号(*)用于检查数组中的 < em > 值 ,而不是数组本身。

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

在上面的例子中:

  • “ name”必须是至少包含3个元素的数组,
  • “ name”数组中的 value 必须是不同的(唯一的)字符串,至少3个字符。

编辑: 由于 Laravel 5.5,你可以像这样直接在 Request 对象上调用 valid()方法:

$data = $request->validate([
"name"    => "required|array|min:3",
"name.*"  => "required|string|distinct|min:3",
]);

您必须遍历输入数组并为每个输入添加规则,如下所述: 循环规则

这里有一些代码给你:

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

我将这个数组作为来自 HTML + V ue.js 数据网格/表的请求数据:

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

并使用这个来验证哪些工作是正确的:

$this->validate($request, [
'*.item_id' => 'required|integer',
'*.item_no' => 'required|integer',
'*.size'    => 'required|max:191',
]);

编写验证和授权逻辑的推荐方法是将该逻辑放在单独的请求类中。这样,您的控制器代码将保持干净。

可以通过执行 php artisan make:request SomeRequest创建请求类。

在每个请求类的 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
]
];
}

在你的控制器中写下你的路由函数如下:

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

每个请求类都带有验证前和验证后的钩子/方法,这些钩子/方法可以根据业务逻辑和特殊情况进行定制,以便修改请求类的正常行为。

您可以为类似类型的请求(例如 webapi)创建父请求类,然后在这些父类中封装一些常见的请求逻辑。

更复杂一点的数据,混合了@Laran 和@Nisal Gunawardana 的答案

[
{
"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
}
]

验证规则将是

 return [
'*.customer_id' => 'required|numeric|exists:customers,id',
'*.foodItemsList.*.id' => 'required|exists:food_items,id',
'*.foodItemsList.*.quantity' => 'required|numeric',
];

下面的代码为我工作的数组来自 ajax 调用。

  $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);
}

在我的情况下,它工作得很好

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