Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of implementation validation rules in a model extending #516

Closed
igor-tv opened this issue Mar 24, 2021 · 2 comments
Closed

Comments

@igor-tv
Copy link
Contributor

igor-tv commented Mar 24, 2021

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

@daftspunk
Copy link
Member

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
Copy link
Contributor Author

igor-tv commented Mar 25, 2021

Yes thank you. This is what I need.

@igor-tv igor-tv closed this as completed Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants