Laravel 5.5使用自定义消息进行验证

时间:2018-03-22 15:04:13

标签: laravel laravel-5 laravel-5.5

我正在使用我的laravel应用程序中的pw更改表单。我想将验证器与自定义错误消息一起使用。

我的代码如下所示:

  $rules = [
    'username' => 'required|max:255',
    'oldpassword' => 'required|max:255',
    'newpassword' => 'required|min:6|max:255|alpha_num',
    'newpasswordagain' => 'required|same:newpassword',
  ];
  $messages = [
     'username.required' => Lang::get('userpasschange.usernamerequired'),
     'username.max:255' => Lang::get('userpasschange.usernamemax255'),
     'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
     'oldpassword.max:255' => Lang::get('userpasschange.oldpasswordmax255'),
     'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
     'newpassword.min:6' => Lang::get('userpasschange.newpasswordmin6'),
     'newpassword.max:255' => Lang::get('userpasschange.newpasswordmax255'),
     'newpassword.alpha_num' => Lang::get('userpasschange.newpasswordalpha_num'),
     'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
     'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
 ];

  $validator = Validator::make($request->all(), $rules, $messages);
  $validator->setCustomMessages($messages);

  Log::debug("custommessages: " . json_encode($messages));
  Log::debug("messages: " . json_encode($validator->messages()));

在日志中 custommessages 会显示我的自定义信息,但在下一行中会显示原始的消息

我正在使用official doc

有人遇到这个问题吗?

提前给出答案!

4 个答案:

答案 0 :(得分:8)

重写和推荐的方法。 参考手册https://laravel.com/docs/5.5/validation#creating-form-requests

使用请求文件。

  1. 运行php artisan make:request UpdateUserPasswordRequest
  2. 写入请求文件
  3.   

    namespace App\Http\Requests;
    
    class UpdateUserPasswordRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            // only allow updates if the user is logged in
            return \Auth::check();
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'username' => 'required|max:255',
                'oldpassword' => 'required|max:255',
                'newpassword' => 'required|min:6|max:255|alpha_num',
                'newpasswordagain' => 'required|same:newpassword',
            ];
        }
    
        /**
         * Get the validation attributes that apply to the request.
         *
         * @return array
         */
        public function attributes()
        {
            return [
                'username'            => trans('userpasschange.username'),
                'oldpassword'             => trans('userpasschange.oldpassword'),
                'newpassword'             => trans('userpasschange.newpassword'),
                'newpasswordagain'       => trans('userpasschange.newpasswordagain'),
            ];
        }
    
        /**
         * Get the validation messages that apply to the request.
         *
         * @return array
         */
        public function messages()
        {
    // use trans instead on Lang 
            return [
         'username.required' => Lang::get('userpasschange.usernamerequired'),
         'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
         'oldpassword.max:255' => Lang::get('userpasschange.oldpasswordmax255'),
         'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
         'newpassword.min:6' => Lang::get('userpasschange.newpasswordmin6'),
         'newpassword.max:255' => Lang::get('userpasschange.newpasswordmax255'),
         'newpassword.alpha_num' =>Lang::get('userpasschange.newpasswordalpha_num'),
         'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
         'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
          'username.max:255' => 'The :attribute field must  have under 255 chars',
            ];
        }
    
    1. 在UserController中
    2. <?php namespace App\Http\Controllers;
      
      
      // VALIDATION: change the requests to match your own file names if you need form validation
      use App\Http\Requests\UpdateUserPasswordRequest as ChangePassRequest;
      //etc
      
      class UserCrudController extends Controller
      {
      public function chnagePassword(ChangePassRequest $request)
      {
       // save new pass since it passed validation if we got here
      }
      }
      

答案 1 :(得分:4)

对于Laravel 7.x 6.x 5.x
定义了自定义规则后,您可以在控制器验证中使用它,例如:

$validatedData = $request->validate([
       'f_name' => 'required|min:8',
       'l_name' => 'required',
   ],
   [
      'f_name.required'=> 'Your First Name is Required', // custom message
      'f_name.min'=> 'First Name Should be Minimum of 8 Character', // custom message
      'l_name.required'=> 'Your Last Name is Required' // custom message
   ]
);

对于本地化,您可以使用:

['f_name.required'=> trans('user.your first name is required'],

希望这对您有帮助...

答案 2 :(得分:0)

Validator::make

中指出消息后
$validator = Validator::make($request->all(), $rules, $messages);

你不应该再说一遍

$validator->setCustomMessages($messages); // don't do that

注意

更好使用请求验证的方法是移动them to another file

答案 3 :(得分:-1)

$messages = [
     'username.required' => Lang::get('userpasschange.usernamerequired'),
     'username.max' => Lang::get('userpasschange.usernamemax255'),
     'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
     'oldpassword.max' => Lang::get('userpasschange.oldpasswordmax255'),
     'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
     'newpassword.min' => Lang::get('userpasschange.newpasswordmin6'),
     'newpassword.max' => Lang::get('userpasschange.newpasswordmax255'),
     'newpassword.alpha_num' => Lang::get('userpasschange.newpasswordalpha_num'),
     'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
     'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
 ];

尝试不要使用:255和:6结尾。


错误:

'username.max:255' => Lang::get('userpasschange.usernamemax255'),

正确:

'username.max' => Lang::get('userpasschange.usernamemax255'),