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

标签 laravel laravel-5 laravel-5.5

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

我的代码如下所示:

  $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()));

在日志中 自定义消息是显示我的自定义消息,但在下一行中有原始 留言 .

我在 official doc 工作.

有人遇到过这个问题吗?

谢谢提前回答!

最佳答案

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

使用请求文件。

  • 运行 php artisan make:request UpdateUserPasswordRequest
  • 写入请求文件

  • 2020 年 2 月编辑:在最新版 Laravel 的授权方法中,可以使用全局 auth() 对象代替\Auth,因此\Auth::check() 将变为 auth()->check()。如果从框架中删除某些内容,两者仍然有效并且会更新

    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();
            // edit you can now replace this with 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' => 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'),
          'username.max' => 'The :attribute field must  have under 255 chars',
            ];
        }
    

  • 在用户 Controller 中

  • <?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
    }
    }
    

    关于Laravel 5.5 使用自定义消息进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49432025/

    相关文章:

    php - Laravel:SQLSTATE [23000]:违反完整性约束:1452 无法添加或更新子行:外键约束失败

    php - Laravel 图像上传插入临时位置和文件名而不是正确的名称

    php - 同一模型中的多对多

    php - 如何在同一个域上运行多个 Laravel 实例?

    php - 从 Laravel 中的 mysql 表中检索值

    php - Laravel: Eloquent 'more than' 和 'lesser than'

    php - 已领取则不领取记录,Laravel 5.7(测验系统)

    laravel - Vuejs 模态组件无法在 axios 调用后发出

    android - 如何使用 Laravel 开发移动应用程序的后端(iOS 和 Android)?任何教程?

    php - Laravel 5.5 - Eloquent - 从具有 "microseconds"字段的 MySQL 表中检索 "datetime(6)"