我正在Laravel开发一个测验应用程序,我在数组验证方面遇到了一些问题。我在前端使用AngularJS,并使用ajax将对象发送到LaravelAPI。这是一个示例 JSON 对象:
{"name":"TestName","category":"TestCategory","questions":[{"answerswers":[{"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'
]);
名称和类别验证工作正常。第三个验证("问题=>"必需")也可以正常工作。其余的验证不执行任何操作。例如
{"name":"SomeName","category":"SomeCategory","questions":[{}]}
通过验证,尽管问题数组具有没有答案或question_text字段的元素。数组验证如何工作?
这是一个已知问题。
有一个打开的拉取请求用于解决"必需"验证。您可以在此处关注此拉取请求。
还有第二个拉取请求,用于解决"required_*"验证(required_with等)的问题。您可以在此处关注该拉取请求。