我正在用 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"}]}
测验有名称、类别和问题。每个问题都必须有问题文本和答案。每个答案都有类型、信息和 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":[{}]}
虽然 questions 数组有一个元素没有答案或 question_text 字段,但通过了验证。数组验证如何工作?
最佳答案
这是一个已知问题。
有一个解决“必需”验证的开放拉取请求。可以关注这个pull request here .
还有第二个拉取请求解决了“required_*”验证(required_with 等)的问题。你可以关注那个pull request here .
关于php - Laravel 5.2 中的数组验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34931497/