Friday, March 10, 2017

OctoberCMS How to Override Users Plugin onRegister() Function?

I'm using OctoberCMS based on Laravel.

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

A previous answer helped me extend the plugin.

I want to restrict Usernames to alphanumeric only with alpha_dash and limit to 50 characters.

The original function in Account.php

public function onRegister()
{
...
    if ($this->loginAttribute() == UserSettings::LOGIN_USERNAME) {
        $rules['username'] = 'required|between:2,255';
    }

My Override

Users Events docs https://github.com/rainlab/user-plugin#events

public function boot() {

    \RainLab\User\Models\User::extend(function($model) {

        $model->bindEvent('model.beforeUpdate', function() use ($model) {

            # User Register
            \Event::listen('rainlab.user.register', function($user, $data) {

                if ($this->loginAttribute() == UserSettings::LOGIN_USERNAME) {
                    $rules['username'] = 'required|alpha_dash|between:2,50';
                }

            });
        }); 
    }); 
}

Error

"Call to undefined method [loginAttribute]"

If I remove the if statement and loginAttribute and use only $rules['username'], I am still able to register names with non-alphanumeric characters.

I have been able to extend new code using this, but not override existing code.



via Matt McManis

Advertisement