Laravel 5:请求验证多维数组 [英] Laravel 5: Request validate multidimensional array
本文介绍了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',
];
}
但这没用.
推荐答案
免责声明:此解决方案由 Alexej 发布在问题中.由于不应在问题正文中共享答案,并且OP似乎处于非活动状态,因此我将他的答案重新发布为社区Wiki,以供将来的读者使用:
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:
我已经找到了解决方案,方法是获取幻灯片数组并在其中循环遍历.
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:请求验证多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文