Laravel 数组验证

Laravel Array Validation

$rules = [
            'user_id'           => 'required|exists:users,id',
            'preparat_id'       => 'required|exists:preparats,id',
            'zoom'              => 'required|numeric',
            'comment'           => '',
            'type'              => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,
            'point_x'           => 'array|required|numeric',
            'point_y'           => 'array|required|numeric',
        ];

        $this->validate($request, $rules);

point_xpoint_y 是数组输入。

我的规则是:

point_xpoint_y 必须存在。

我如何发送数据:



point_x[0] = 123; point_y[0] = "SO";

错误


point_y[0] = 123;

错误


错误

我的Laravel版本是5.4

我应该如何写一个像上面这样的检查规则。我尝试了数组参数,但它不起作用。

  public function rules($array)
    {
      $rules = [  
            'user_id'           => 'required|exists:users,id',
            'preparat_id'       => 'required|exists:preparats,id',
            'zoom'              => 'required|numeric',
            'comment'           => '',
            'type'              => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,];

      foreach($array as $key => $val)
      {
        $rules[$key] = 'required|numeric';
      }
      return $rules;
    }

仅尝试遵循数组 point_xpoint_y 的规则,

$this->validate($request, [
    'point_x' => 'required',
    'point_y' => 'required',
    'point_x.*' => 'numeric',
    'point_y.*' => 'numeric',
], 
     $messages = [

     ]
);

查看文档 Array Input Validation