dongxiang5879 2017-09-18 17:02
浏览 64
已采纳

Laravel Eloquent验证请求的项目字段foreach

I am writing Validator in Laravel, and so I am getting json format request.

I have written validate logic for main json's fields such as id, name... Now I have items array of objects in json, and I need to validate each item from this array which is passed.

Here's example of json request: https://api.myjson.com/bins/ob3lh

And here's my validator so far:

 private function update()
  {
    return [
      'id' => 'required',
      'place_id' => 'required',
      'place_table_id' => 'required',
      'user_id' => 'required',
      'seen' => 'required',
      'state' => 'required',
      'number' => 'required',
      'date' => 'required',
      'price' => 'required',
      'table_number' => 'required',
      'note' => 'required',
    ];
  }

How can I add simply for example nested validator which will validate each of items object?

  • 写回答

1条回答 默认 最新

  • doujiang2643 2017-09-18 17:10
    关注

    You can define nested validation rules like this:

    private function update()
    {
        return [
            'id' => 'required',
            'place_id' => 'required',
            'place_table_id' => 'required',
            'user_id' => 'required',
            'seen' => 'required',
            'state' => 'required',
            'number' => 'required',
            'date' => 'required',
            'price' => 'required',
            'table_number' => 'required',
            'note' => 'required',
            'items' => 'required|array|min:1',
            'items.*.id' => 'required',
            'items.*.name' => 'required',
            'items.*.amount' => 'required',
            'items.*.price' => 'required',
        ];
    }
    

    You can find more info on this in the docs: https://laravel.com/docs/5.5/validation#validating-arrays

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?