Rainlab User extension email rule

Hello,

i have a scenario where i am using the rainlab users with username. There are users who have the same emails but in the Users Model the validation is

public $rules = [
        'email' => 'required|between:3,255|email|unique:users',
    ];

I want to remove the “unique:users” filters. I used the following

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

            $model->bindEvent('model.beforeSave', function() use ($model) {
                $validator = \Validator::make($model->attributes, [
                    'email' => 'required|between:3,255|email',
                ]);
            });
        });

But it is still using the unique filter. Thanks in advance.

Can you try this?

\RainLab\User\Models\User::extend(function($model) {
    $model->bindEvent('model.beforeSave', function() use ($model) {
        $model->rules['email'] = 'required|between:3,255|email';
    });
});

@apinard thanks alot. but unfortunately not working.

\RainLab\User\Models\User::extend(function($model) {
    $model->bindEvent('model.beforeValidate', function() use ($model) {
         $model->rules['email'] = 'required|between:3,255|email';
    });
});

?

2 Likes

The last snippet from @apinard using model.beforeValidate should work. Also, try unset to confirm the event is working:

unset($model->rules['email']);
1 Like