laravel自定义验证

// 输入验证
       $rules = [
           ’email’ => ‘required|unique:posts|max:255’,
           ‘name’ => ‘required|between:1,20’,
           ‘body’ => ‘required’,
       ];
       $message = [
           ‘required’ => “:attribute 不能为空”,
           “between” => “:attribute 长度必须在 :min 和 :max 之间”
       ];
       $attributes = array(
           “email” => ‘电子邮件’,
           ‘name’ => ‘用户名’,
           ‘body’ => ‘内容’,
       );

       $validator = Validator::make($request->all(), $rules, $message, $attributes);

       if ($validator->fails()) {

           return response()->json([‘code’ => ‘-1’, ‘message’ => $validator->errors()->all()]);
       }

Leave a Reply

Your email address will not be published. Required fields are marked *