我正在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","is_correct":false,"$$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字段的元素。阵列验证如何工作?
答案 0 :(得分:3)
这是一个已知问题。
有一个打开拉取请求,可以解决“必需”验证问题。您可以按照pull request here。
进行操作还有第二个pull请求解决了“required_ *”验证问题(required_with等)。您可以关注pull request here。