This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

spammers5581
spammers5581

I am using the Rainlab User plugin and so far it's working. The validation rules in the RainLab\User\Models\User model are these:

public $rules = [
       'email' => 'required|between:3,64|email|unique:users',
       'username' => 'required|between:2,64|unique:users',
       'password' => 'required:create|between:4,64|confirmed',
       'password_confirmation' => 'required_with:password|between:4,64'
   ];

Now let's say that my application requires the name attribute as well. Where would I add this to make it work without overwriting the plugin's User model itself?

mja
mja

Read this: Extending models

Last updated

spammers5581
spammers5581

I've read this, but I don't understand how it helps - how would I manipulate the $rules property of the model and where would I write this User::extend() call? I tried modifying my "user profile" page like this:

function onStart() {
    \RainLab\User\Models\User::extend(function($model){
        $myrules = $model->rules;
        $myrules['name'] = 'required';
        $model->rules = $myrules;
    });
}

But it doesn't work, if I dump the $rules property of the model later on in the request, I don't see mine.

Last updated

Eoler
Eoler

User is usually extended in plugin's boot() method, like this: https://github.com/rainlab/userplus-plugin/blob/master/Plugin.php#L35

Documentation states how to dynamically extend validation rules: https://octobercms.com/docs/database/traits#dynamic-validation-rules

Together it gives:

UserModel::extend(function($model) {
    $model->bindEvent('model.beforeValidate', function() use ($model) {
        $model->rules['surname'] = 'required';
    });
});

A bit late, but here it is...

Webp
Webp

Thank you Eoler!

Webp
Webp

Eoler said:

User is usually extended in plugin's boot() method, like this: https://github.com/rainlab/userplus-plugin/blob/master/Plugin.php#L35

Documentation states how to dynamically extend validation rules: https://octobercms.com/docs/database/traits#dynamic-validation-rules

Together it gives:

UserModel::extend(function($model) {
   $model->bindEvent('model.beforeValidate', function() use ($model) {
       $model->rules['surname'] = 'required';
   });
});

A bit late, but here it is...

This would also work:

UserModel::extend(function($model) {
    $model->rules['surname'] = 'required';
});
Hidden Raven
Hidden Raven

Hello everyone, I need help, and since it's highly related to this thread I'll post here. I'd like to change the default validation messages and add validation for my two new fields.

And it works partially, the validation for my 2 new fields and my customMessages only works if the two "default" required fields are filled up (email and password).

Here's the code:

UserModel::extend(function ($model){
    $model->addFillable([
        'zipcode',
        'phone'
    ]);

    $model->rules['zipcode'] = 'required';
    $model->rules['phone'] = 'required';
    $model->rules['name'] = 'required';

    $model->customMessages['email.unique'] = 'Un compte existe déjà avec cette adresse';
    $model->customMessages['required'] = 'Ce champs est requis';

});

Can you help me ? Thanks.

pain-spark50564
pain-spark50564

mja said:

Read this: Extending models

Nice man, instead help him with a answer you make him read the doc, very nice humam. You already think, that have many people that learn better doing than reading ?

jimlab56999
jimlab56999

Hello,
I get validation message all the time if the field are empty or not
What am I doing wrong?
Code:

class Plugin extends PluginBase
{
    use \October\Rain\Database\Traits\Validation;

...

 public function boot()
    {
        RealtyModel::extend(function ($model) {
            $model->hasOne['addproperties'] = ['Eduard\AddProperties\Models\AddProperties'];
            $model->rules = ['floor' => 'required'];
        });

Thank you

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.