本文介绍了Laravel 5:请求验证多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have forms that submit multidimensional arrays.Like:

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

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

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

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

但这没用.

推荐答案

我已经找到了解决方案,方法是获取幻灯片数组并在其中循环遍历.

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

这篇关于Laravel 5:请求验证多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 12:52