How To Change Forms Dynamically Based On Radiobutton Selection In Yii

I’m new to Yii framework and I’m designing a form (I’m using create form) to create a row in database.

Let me explain about the scenario succintly, so that I can make it clear for what I want-

I have 10 fields in this form. Out of this 10 fields, five fields change dynamically.

I created two div’s basically div A and div B and repeated the fields as required for the two cases.

Say some fields which are textfields in div A will be dropdownlists in div B.




<div id="A">

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

<?php echo $form->textArea($model,'selectionList',array('rows'=>6, 'cols'=>50)); ?>

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

</div>  


<div id="B">

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

<?php echo $form->dropdownList($model,'selectionList',array('rows'=>6, 'cols'=>50)); ?>

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

</div>




I have two radiobuttons say Single and Multi. When I select Single radiobutton, div A should be considered and div B discarded and when I select Multi, vice versa should happen.

So, is this the right way to change the form fields dynamically. Or else how can I do this using Ajax validation.

This is not Yii specific.

Search the web for ‘jquery toggle div radiobutton’.

You will find for example:

Please use the below code

  1. call a ajax on radio button



<div class="row">

<?php 


$form->radioButtonList($model, 'filed', array(0=>'Single',1=>'Multi'),

	array(

		'separator'=>'', 

		'labelOptions'=>array('style'=>'display:inline'), 

		"async"=>false, 

		'onclick' =>CHtml::ajax(array(

                                  "type"=>"GET", 

                                  "url"=>array("registration/add"), 

                                  "data"=>array('test'=>'js:this.value'), 

                                  "update"=>"#test"

                            ))

			)

		);

?>

</div>

<div id="test"></div>

  1. In controller action



public function add(){

   $value =$_GET['test']

   if($value==1){

		//A div

   }else{

		//B div

   }                

   	

}

I hope it;s some help