php – Laravel 5.2中的数组验证
发布时间:2020-12-14 19:42:44 所属栏目:大数据 来源:网络整理
导读:我在Laravel开发一个测验应用程序,我在 array validation上遇到了一些问题.我在前端使用AngularJS,我使用ajax将对象发送到Laravel API. 这是一个示例JSON对象: {"name":"TestName","category":"TestCategory","questions":[{"answers":[{"type":"radio","in
我在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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |