Add an example of implementation validation rules in a model extending
igor-tv opened this issue · 2 comments
igor-tv commented
There is a lack of information on how to validate the added fields to the model. Is this implemented using the addDinamycProperty()
method?
daftspunk commented
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']);
}
});
});
igor-tv commented
Yes thank you. This is what I need.