laravel框架validator验证使用

private $validate = [
        //规则
        'rule' => [
            'name'   => 'required | min:3 | max:20 | string | unique:book',
            'author' => 'required | min:2 | max:20 | string',
            'sort'   => 'required | integer',
        ],
        //提示信息
         'message' => [
             'required' => ':attribute不能为空',
             'min'      => ':attribute字数太少了',
             'max'      => ':attribute字数太多了',
             'string'   => ':attribute格式错误',
             'integer'   => ':attribute必须为数字',
             'unique'   => '该:attribute已经存在',
         ],
        //自定义
        'custom' => [
            'name'   => '书名',
            'author' => '作者',
            'sort'   => '排序'
        ]
    ];
$validator = Validator::make(
            $request->all(),
            $validate['rule'],
            $validate['message'],
            $validate['custom']
        );
if ($validator->fails()) {
      return response()->json([
          'status'=>'0',
           'info' => $validator->errors()->first()
      ]);
}
 
 
 

猜你喜欢

转载自blog.csdn.net/gyen_ki/article/details/78386815