octobercms/docs

Add an example of implementation validation rules in a model extending

igor-tv opened this issue · 2 comments

There is a lack of information on how to validate the added fields to the model. Is this implemented using the addDinamycProperty() method?

I've added this to the docs. Is it what you're after?


To add model validation for introduced fields, hook into the beforeValidate event and throw a ValidationException exception.

User::extend(function ($model) {
    $model->bindEvent('model.beforeValidate', function () use ($model) {
        if (!$model->billing_first_name) {
            throw new \ValidationException(['billing_first_name' => 'First name is required']);
        }
    });
});

Yes thank you. This is what I need.