S
S
Sergey Beloventsev2018-03-09 23:19:48
Yii
Sergey Beloventsev, 2018-03-09 23:19:48

How to use $model->getErrors() in an exception?

in fact, the whole question is, someone has their own secrets, let's say this code

if($model->save())
            throw new \RuntimeException($model->getErrors());

falls out with an error, but the array can contain any code, for example
'Title'=>[
     0=> ' поле Title должно быть строкой']

Here I have a question and someone has their favorite methods for getting errors.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-03-10
@webinar

the question is why exactly \RuntimeException?
i wrote a widget, inserted it into crud generator and forgot about this problem. I just display $model->getErrors()it in the view, through the standard bootstrap alert (actually a little different, which is in adminLTE).
But if you want globally and through errors, then you can make your own base model and put this method in afterValidate and then inherit all models from it. Well, or how to connect the behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question