Laravel 5:请求验证多维数组

时间:2023-01-26 14:47:57

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.

但它没有用。

2 个解决方案

#1


6  

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似乎不活跃,我将其答案作为社区维基重新发布给未来的读者:

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

#2


0  

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']相同。知道如何将这些参数发送到后端是有益的,然后能够为您提供有关如何设置验证的线索。

#1


6  

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似乎不活跃,我将其答案作为社区维基重新发布给未来的读者:

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

#2


0  

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']相同。知道如何将这些参数发送到后端是有益的,然后能够为您提供有关如何设置验证的线索。