在 Laravel 5.7 中验证表单中的输入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52404515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:04:17  来源:igfitidea点击:

Validate array of inputs in form in Laravel 5.7

phplaravellaravel-5laravel-validationlaravel-5.7

提问by Dilani

My form has the same input field multiple times. My form field is as follows:

我的表单多次具有相同的输入字段。我的表单字段如下:

<input type='text' name='items[]'>
<input type='text' name='items[]'>
<input type='text' name='items[]'>

And request contains ($request['items'):

并且请求包含 ($request['items'):

array:1 [▼
  "items" => array:3 [▼
    0 => "item one"
    1 => "item two"
    2 => "item three"
  ]
]

I want atleast one of the items to be filled. My current validation in the controller is

我希望至少填写其中一项。我当前在控制器中的验证是

    $validator = Validator::make($request->all(),[
        'items.*' => 'required|array|size:1'
    ]);

It does not work. I tried with combination of size, required, nullable. Nothing works.

这是行不通的。我尝试了大小、必需、可为空的组合。没有任何作用。

回答by Exterminator

You can check it like this:

你可以这样检查:

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

In the example above:

在上面的例子中:

  • "items" must be an array with at least 1 elements.
  • Values in the "items" array must be distinct (unique) strings, at least 1 characters long.
  • “items”必须是至少包含 1 个元素的数组。
  • “items”数组中的值必须是不同(唯一)的字符串,长度至少为 1 个字符。

回答by Marcin Nabia?ek

In fact, it's enough to use:

事实上,使用它就足够了:

$validator = Validator::make($request->all(),[
        'items' => 'required|array'
    ]);

The changes made:

所做的更改:

  • use itemsinstead of items.*- you want to set rule of general items, if you use items.*it means you apply rule to each sent element of array separately
  • removed size:1because it would mean you want to have exactly one element sent (and you want at least one). You don't need it at all because you have requiredrule. You can read documentation for required ruleand you can read in there that empty array would case that requiredrule will fail, so this requiredrule for array makes that array should have at least 1 element, so you don't need min:1or size:1at all
  • 使用items而不是items.*- 你想设置一般项目的规则,如果你使用items.*它意味着你将规则分别应用于数组的每个发送元素
  • 删除,size:1因为这意味着您只想发送一个元素(并且您至少需要一个)。你根本不需要它,因为你有required规则。您可以阅读所需规则的文档,你可以阅读那里空数组会情况required的规则将失效,所以这个required规则阵列使得该阵列应该至少有1元,所以你不需要min:1size:1根本

回答by Jan Richter

Knowing you are using the latest version of Laravel, I really suggest looking into Form Request feature. That way you can decouple validation from your controller keeping it much cleaner.

知道您使用的是最新版本的 Laravel,我真的建议您查看表单请求功能。通过这种方式,您可以将验证与控制器分离,使其更加简洁。

Anyways as the answer above me suggested, it should be sufficient for you to go with:

无论如何,正如我上面的答案所建议的那样,你应该足够了:

'items' => 'required|array'

回答by Surya Neupane

Just Do it normally as you always do:

像往常一样照常做:

 $validator = Validator::make($request->all(),[
    'items' => 'required'
  ]);

回答by Surya Neupane

You should try this:

你应该试试这个:

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

回答by ourmandave

You can use a custom rule with a closure.

您可以使用带有闭包的自定义规则。

https://laravel.com/docs/5.7/validation#custom-validation-rules

https://laravel.com/docs/5.7/validation#custom-validation-rules

To check if an array has all null values check it with array_filter which returns false if they're all null.

要检查数组是否具有所有空值,请使用 array_filter 检查它,如果它们都为空,则返回 false。

So something like...

所以像...

  $request->validate([

    'items' => [
      // $attribute = 'items', $value = items array, $fail = error message as string
       function($attribute, $value, $fail) {
         if (!array_filter($value)) {
           $fail($attribute.' is empty.');
         } 
       },
     ]
   ]);

This will set the error message: 'items is empty."

这将设置错误消息:“项目为空。”