esroyo / oc-userprofile-plugin

Add user profile fields to October CMS front-end users.
GNU Lesser General Public License v3.0
6 stars 5 forks source link

Question: Is it possible to extend the validation rules? #9

Open Verbat opened 7 years ago

Verbat commented 7 years ago

Is it possible to extend the validation rules to make it more specific? Let's say I create a field in the backend and set it to text, now I also want to limit it to max x characters or compare it to another field.

I'm perfectly fine with extending the rules at the page code section but I'm not quite sure where to start. Would be great if you can point me in the right direction or show how to do it as with the build in validation there are a lot of possibilities where this might be usefull. Thanks

adc91 commented 5 years ago

Try extending the model in the Plugin.php inside the boot function.

use RainLab\User\Models\User;

public function boot()
{
    User::extend(function($model) {
        $model->bindEvent('model.beforeValidate', function() use ($model) {
            $model->addFillable([
                'custom_field',
            ]);

            $model->rules = [
                'custom_field' => ['required']
            ];
        });
    });
}