Validation in form fields rendered via Ajax

Hi, i have a form with 2 different models, and 1 of these models can have multiple rows inside the form.

I managed to do that with this code called by a button firing an ajax action:

        ob_start(); $form = ActiveForm::begin(); ob_get_clean();

        ob_start(); ActiveForm::end(); ob_get_clean();

        return $this->renderAjax('_foo', [

            'model' => $model,

            'form' => $form,

            'index' => $index,


The foo view:

<?= $form->field($model, "[$index]field_name")->textInput()->label(FALSE) ?>

This works fine, i can submit the form and get the input inside the Post.

However, what i miss is javascript validation.

While the first row does it, every row added with Ajax doesn’t.

I tried adding enableAjaxValidation, enableClientValidation ecc. to the form and to the fields, but it doesn’t work either.

How can i do that?

Another question, where can i find the documentation for yii.activeForm.js ?

Thanks in advance

if you look at the source html of the activeform rendered page, you will find some javascript code, which do all the work for this client validation.

In your ajax code, only html is received from the server, javascript required for the client validation is not there. So, if you want client validation, one of the ways is:

  • render the activeform in the view page and hide it.

  • whenever you require the form, instead of ajax, show it using jquery etc.

Yeah, i noticed that.

Yeah, it’s a solution, but i’d have to use a fixed number of rows.

I want to let the user add any number of rows.