php – Laravel 5.2中的数组验证

前端之家收集整理的这篇文章主要介绍了php – Laravel 5.2中的数组验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Laravel开发一个测验应用程序,我在 array validation上遇到了一些问题.我在前端使用AngularJS,我使用ajax将对象发送到Laravel API.
这是一个示例JSON对象:

{"name":"TestName","category":"TestCategory","questions":[{"answers":[{"type":"radio","information":"Test answer two","is_correct":false,"$$hashKey":"object:28"},{"type":"radio","information":"Test answer One","$$hashKey":"object:22"}],"$$hashKey":"object:13","question_text":"Test Question One"}]}

测验有名称,类别和问题.每个问题都必须有question_text和答案.每个答案都有类型,信息和is_correct.

这是我写的验证:

$this->validate($request,[
            'name' => 'required|min:3','category' => 'required|min:2','questions' => 'required','questions.*.question_text' => 'required|min:5','questions.*.answers' => 'required'

        ]);

名称和类别验证工作正常.第三个验证(‘questions =>’required’)也可以正常工作.其余的验证什么都不做.
例如,

{"name":"SomeName","category":"SomeCategory","questions":[{}]}

虽然问题数组有一个没有答案或question_text字段的元素,但验证通过验证.阵列验证如何工作?

解决方法

这是一个已知的问题.

有一个打开拉取请求,解决“必需”验证.你可以关注这个pull request here.

还有第二个pull请求通过“required_ *”验证(required_with等)来解决问题.你可以按照pull request here.

猜你在找的Laravel相关文章