Laravel 5:请求验证多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32092276/
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 12:12:57  来源:igfitidea点击:

Laravel 5: Request validate multidimensional array

arraysvalidationlaravelmultidimensional-arrayrequest

提问by Alexej

I have forms that submit multidimensional arrays. Like:

我有提交多维数组的表单。喜欢:

slide[1][title]
slide[2][title]

Now I use a Request class to define my rules. How can I loop through all array items within this class. I tried:

现在我使用 Request 类来定义我的规则。如何循环遍历此类中的所有数组项。我试过:

public function rules()
{
    return [
        'id' => 'required',
        'slide' => 'array|min:1',
        'slide.*.title' => 'required|max:255',
        'slide.*.description' => 'required|max:255',
    ];
}

But it did not work.

但它没有用。

采纳答案by totymedli

Disclaimer:This solution was posted in the question by Alexej. Since answers shouldn't be shared in the question body and the OP seems to be inactive, I repost his answer as a community wiki for future readers:

免责声明:此解决方案由Alexej在问题中发布。由于不应在问题正文中共享答案并且 OP 似乎处于非活动状态,因此我将他的答案作为社区 wiki 重新发布,供未来读者使用:

I've found the solution by getting the slide array and loop through it.

我通过获取幻灯片数组并循环遍历它找到了解决方案。

public function rules()
{
    $rules = [
        'id' => 'required',
        'slide' => 'array|min:1',
    ];
    foreach($this->request->get('slide') as $key => $val){
        $rules['slide.'.$key.'.title'] = 'required|max:255';
        $rules['slide.'.$key.'.description'] = 'required|max:255';
    }
    return $rules;
}

回答by Pablo Ezequiel Leone

There's no preconfigured validation rule for multidimensional arrays. The easist way is to do the array validation inside your controller.

多维数组没有预配置的验证规则。最简单的方法是在控制器内部进行数组验证。

The problem is when you use multidimensional array to store single values, then the logic is wrong and what you should fix is your logic, not the framework.

问题是当您使用多维数组存储单个值时,逻辑是错误的,您应该修复的是您的逻辑,而不是框架。

For instance, I saw lots of time sending the user credentials like $var['login']['pass'] and $var['login']['username'], which it could be translated easily to 2 different variables, which would make more sense.

例如,我看到很多时间发送用户凭据,如 $var['login']['pass'] 和 $var['login']['username'],它们可以轻松转换为 2 个不同的变量,这会更有意义。

In case you know what those values should be and you feel confident that the validation could be something generic for all different values, you can create a custom validator (read validation documentation of your laravel version).

如果您知道这些值应该是什么并且您确信验证可以是所有不同值的通用内容,您可以创建一个自定义验证器(阅读您的 laravel 版本的验证文档)。

Referring to your code I think multidimensional array is declared the same way as in your html slide[]['title']. It'll be beneficial to know how you are sending those parameters to the backend, to then be able to give you a clue about how to set up the validation.

参考您的代码,我认为多维数组的声明方式与您的 html 中的声明方式相同slide[]['title']。了解如何将这些参数发送到后端会很有帮助,然后能够为您提供有关如何设置验证的线索。