R
R
romany42014-05-28 18:41:45
Yii
romany4, 2014-05-28 18:41:45

How to display validation errors from multiple forms in yii2?

There are 2 forms on one page. How to make these 2 forms display errors to the user in case of incorrect data?

if ($modelLog->load(Yii::$app->request->post()) && $modelLog->validate() && 
$model->load(Yii::$app->request->post()) && $model->validate()) 
{
...
}

With the current code, validation errors are displayed only for the first form ($modelLog). If you swap it, then errors for $model will be displayed accordingly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Morozov, 2014-05-28
@romany4

This is because the and-condition check doesn't go beyond the first false it encounters (in your case, $modelLog->validate()).
To validate both forms at once, modify the validation logic. Something like

$isValid1 = $modelLog->validate();
$isValid2 = $model->validate();
if ($isValid1 && $isValid2) {...}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question