OctoberCMS How to Override Users Plugin onRegister() Function?

前端 未结 1 675
故里飘歌
故里飘歌 2021-01-07 04:58

I\'m using OctoberCMS based on Laravel.

I\'m trying to override the Users Plugin onRegister() function.

A previous answer helped me extend the p

相关标签:
1条回答
  • 2021-01-07 05:10

    I don't think you understand the page cycle here.

    rainlab.user.register is called after the user has already been registered. I.e. they have already passed validation and already exist with the invalid username.

    What you can do instead is bind to the User model's model.beforeSave event and do your own validation of the username:

    public function boot() {
    
        \RainLab\User\Models\User::extend(function($model) {
    
            $model->bindEvent('model.beforeSave', function() use ($model) {
                $validator = \Validator::make($model->attributes, [
                    'username' => 'required|alpha_dash|between:2,50',
                ]);
    
                if ($validator->fails()) {
                    throw new \ValidationException([
                        'username' => 'Username must contain alphanumeric values only, and be between 2 and 50 characters in length',
                    ]);
                }
            });
    
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题