Client validation does not work in Yii2 when the form is rendered using an ajax call

advertisements

Form rendered using ajax:

<?php $form = ActiveForm::begin(['id' => 'que',
        'enableClientValidation' => true
    ]); ?>
            <?php echo $form->field($model, 'fk_subject')
                    ->dropDownList(ArrayHelper::map($subjects, 'id_subject', 'name'), [
                        'class'     => 'form-control ng-pristine ng-valid ng-touched',
                        'ng-model'  => 'que.fk_subject',
                        'prompt'    => 'Select subject',
                        'ng-change' => 'fillTopic(que);'
                    ]); ?>
            <?php echo $form->field($model, 'fk_topic')
                    ->dropDownList([], [
                        'class'     => 'form-control ng-pristine ng-valid ng-touched',
                        'ng-model'  => 'que.fk_topic',
                        'prompt'    => 'Select topic',
                        'ng-change' => 'openAddTopic(que);',
                        'ng-options' => 'topic.id_subject as topic.name for topic in topics',
                    ]); ?>
            <?php echo $form->field($model, 'type')
                    ->dropDownList($questionTypes, [
                        'class'     => 'form-control ng-pristine ng-valid ng-touched',
                        'prompt'    => 'Select question type',
                        'ng-model'  => 'que.type',
                        'ng-change' => 'addAnswerOptions(que);',
                    ]); ?>
                    <?php echo Html::submitButton('Save',
                        ['class' => 'btn btn-primary',
                            'name' => 'Save']) ?>
<?php ActiveForm::end(); ?>

I loaded the above form using ajax in yii2 and problem is client side validation is not working with it. If I load the form content on window load it works fine.


OK I find the solution, I will have to use renderAjax to get the form html or will have to add beginpage and beginend function if I use renderPartial

<?php $this->beginPage(); ?>
<?php $this->beginBody(); ?>
// Form and content redenered using ajax
<?php $this->endBody(); ?>
<?php $this->endPage(); ?>

so that yii can add client validation script with the rendered content