Ajax Form Content Validation

Hi All,

In my form I have dependant dropdown list which does ajax request and adds more elements to the form.

The dropdown code looks like -




    <div class="row">

        <?php echo $form->labelEx($model,'type'); ?>

        <?php echo $form->dropDownList($model,'type', Globals::getTypes(),

                        array(

                            'ajax' => array(

                                'type'      => 'POST',

                                'url'       => Yii::app()->controller->createUrl('getFields'),

                                'update'    => '#fields',

                            )

                        )

                    ); ?>

        <?php echo $form->error($model,'type'); ?>

    </div>



The action code is -




     public function actionGetFields() {

        $type    = $_POST['MyTypes']['type'];

        $typeModelName = Globals::getTypeModelName($type);

        if(!$typeModelName)

            Yii::app()->end();


        $typeModel = new $typeModelName;

        $this->renderPartial('_getFields', array('model'=>$typeModel), false, true);

        Yii::app()->end();

    }



My _getFields view looks like this -


 <div class="row">

        <?php echo CHtml::activeLabel($model,'key'); ?>

        <?php echo CHtml::activeTextField($model,'key',array('size'=>60,'maxlength'=>128)); ?>

    </div>


    <div class="row">

        <?php echo CHtml::activeLabel($model,'txn'); ?>

        <?php echo CHtml::activeTextField($model,'txn',array('size'=>60,'maxlength'=>128)); ?>

    </div>



My whole setup works really fine, except I am not able to validate the fields in _getFields view page.

Please let me know how can I add validation for partially rendered view in errorSummary() of main view and also individual filed should show the error message.

I tried something like this for individual fields -


<?php echo CActiveForm::error($model,'txn'); ?>

but got the error like

Thanks Already,

Sachin.

Anyone… who has faced the same problem?

Adding the form html elements via ajax request and then validating those …?

And what happens if you use ActiveForm elements in your _getFields view? Like this:


    <div class="row">

        <?php echo $form->labelEx($model,'key'); ?>

        <?php echo $form->textField($model,'key',array('size'=>60,'maxlength'=>128)); ?>

        <?php echo $form->error($model,'key'); ?>

    </div>


    <div class="row">

        <?php echo $form->labelEx($model,'txn'); ?>

        <?php echo $form->textField($model,'txn',array('size'=>60,'maxlength'=>128)); ?>

        <?php echo $form->error($model,'txn'); ?>

    </div>

Although I don’t know whether you have to pass the $form variable…

Anyway, you can just as well add the two rows above to the main view, run it, copy the output HTML and paste it into your partial view…

Hi Bennouna,

Thanks for the reply!

I used CActiveForm element in the _getFields and got the fatal error -

This is because, I am not passing "$form" to my ajax handler action and thus not passing to _getFields.

The solution is to pass $form (which is instance of CActiveForm) to partially rendered view ‘_getFields’.

As I mentioned, Dropdown list on ‘onchange’ or ‘onlick’ event gives ajax request to controller action “getFields” and action calls renderPartial on “_getFields” view.

In this scenario, how can I pass CActiveForm element ($form) to my controller action as ajax request parameters?

Please let me kown if there is any other way to do this?

Thanks,

Sachin.

Hi Bennouna,

Thanks for the reply!

At least this way I can see validation errors in errorSummary().

Thanks,

Sachin.