A
A
Alexander Verbitsky2019-01-28 02:18:55
Yii
Alexander Verbitsky, 2019-01-28 02:18:55

How to make Validation work in Yii2?

Why if you insert a form in the form

$form->field($model, 'user')->widget(Select2::classname(), []);

then when you click on the form button, the validation works. Under the field, if it is empty, an error is displayed depending on the conditions in "rules".
And if in this form:
echo Select2::widget(['model' => $model, 'attribute'     => 'user',]);

then the validation crashes, and even an empty form is sent,
what to do with it?
I must say right away that my fields are loaded via ajax, depending on the choice in one select. Therefore, I display everything in the second form, without field.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Bukharev, 2019-01-28
@evgenybuckharev

Describing a field via $form->field(), the process goes through ActiveField, which generates a valid HTML structure from the point of view of client-side validation. Otherwise, the HTML code looks different, and the standard client-side validation cannot work correctly.
By examining the client validation code, you can understand what elements it expects, and by substituting them, you can make the code work. For example, these are the selectors of the fields themselves, the container selectors (filed-*), the selectors of the error placement block.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question