php - Laravel 5.2 中的数组验证

标签 php validation laravel laravel-5 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","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/

相关文章:

javascript - asp.net 和 javascript 中的回发和验证

ruby-on-rails - 插件中的验证方法

php - Laravel 与 Faker 做种

php - Bigcommerce API 更新库存水平(正常库存跟踪和按选项跟踪)

javascript - 如何在 PHP 中添加带有 JS 函数的输入标签

php - 将表单数据作为变量传递给 docker 命令

javascript - 无法使用 Angular 将数据 POST 到 Laravel 5 Controller ,错误 : 422 Unprocessable Entity

javascript - 获取错误消息;资源加载失败: the server responded with a status of 500

c# - ASP 必填字段验证器显示星号而不是错误消息

laravel - 永久缓存,包括服务器重启和 redis 到磁盘功能