Dependent dropdown

Hello,

I try to use dependent dropdown but it’s not working. here is my view and controller. Can you please help me?

View

[code>

<tr>

	&lt;td&gt;&lt;?php echo CHtml::activeLabel(&#036;form,'Region'); ?&gt;&lt;/td&gt;


	&lt;td&gt;&lt;?php echo CHtml::dropDownList('Region_id','', CHtml::listData(Region::model()-&gt;findAll(),'id','region'),


    array(


    'ajax' =&gt; array(


    'type'=&gt;'POST',


    'url'=&gt;CController::createUrl('Rlab'), 


    'update'=&gt;'#Lab_id', 


    )));


    ?&gt;


    &lt;/td&gt;


&lt;/tr&gt;


   &lt;tr&gt;


	&lt;td&gt;&lt;?php echo CHtml::activeLabel(&#036;form,'Lab_id'); ?&gt;&lt;/td&gt;


	&lt;td&gt;&lt;?php 


		echo CHtml::activeDropDownList(&#036;form,'Lab_id',CHtml::listData(Lab_id::model()-&gt;findAll(),'id','name'));?&gt;


	&lt;/td&gt;


&lt;/tr&gt;

</code>

Controller




public function actionRlab()

    {

        $data = Lab::model()->findAll('Region_id=:parent_id',

                        array(':parent_id'=>(int) $_POST['Region_id']));

 

 

        $data = CHtml::listData($data,'id','name');

            foreach($data as $id => $value)

            {

                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);

            }

 

    }




Try to echo your $_POST[‘Region_id’] in the controller. Does it contain a value? To test, you’ll also need to create a new, empty div with a different Id and update that one.

THe lab value is blank. here is my update view and controller. can you please help

<tr>

	&lt;td&gt;&lt;?php echo CHtml::activeLabel(&#036;form,'Region'); ?&gt;&lt;/td&gt;


	&lt;td&gt;&lt;?php echo CHtml::dropDownList('Region_id','', CHtml::listData(Region::model()-&gt;findAll(),'id','region'),


    array(


    'prompt'=&gt;'Please select Region', 


    'ajax' =&gt; array(


    'type'=&gt;'POST',


    'url'=&gt;CController::createUrl('Dlab'), 


    'update'=&gt;'#'.CHtml::activeId(&#036;form,'Lab_id'),


    )));


    ?&gt;


    &lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


	&lt;td&gt;&lt;?php echo CHtml::activeLabel(&#036;form,'Lab_id'); ?&gt;&lt;/td&gt;


	&lt;td&gt;&lt;?php 


		 echo CHtml::activeDropDownList(&#036;form,'Lab_id',array());?&gt;


	&lt;/td&gt;


&lt;/tr&gt;

AND THE CONTROLLER

public function actionDlab()

{


    &#036;data = Lab::model()-&gt;findAll('parent_id=:parent_id',


                    array(':parent_id'=&gt;(int) &#036;_POST['Lab_id']));








    &#036;data = CHtml::listData(&#036;data,'id','name');


        foreach(&#036;data as &#036;id =&gt; &#036;value)


        {


            echo CHtml::tag('option',array('value' =&gt; &#036;id),CHtml::encode(&#036;value),true);


        }


}

My dependent dropdown is working but If I use CHTML::activeDropDownList instead as per cookbook http://www.yiiframework.com/wiki/24/ I get validation error that field cannot be blank. Can you tell me

how to solve this





<td>

	        <?php 

	        

	        $list=CHtml::listData(Region::model()->findAll(),'id','region'); 

            echo CHtml::dropDownList($model,'Region_id',$list, 

            

            array( 

           'empty'=>'Please Select Region', 

           'ajax' => array( 

           'type'=>'POST', //request type 

           'url'=>CController::createUrl('test/dynamicRegion'), //url to call. 

           'update'=>'#Lab_id', //selector to update 

           )));

           

           ?>

          

        </td>

     </tr>

     <tr>

     	<td><?php echo $form->labelEx($model,'Lab_id');?></td>

     	<td> 

            <?php 

            	//echo CHtml::activeDropDownList($model,'Lab_id',array());

                        //empty since it will be filled by the other dropdown 

              echo CHtml::activeDropDownList($model,'Lab_id', array(),array('id'=>'Lab_id'));                 

            ?>

        </td>

     </tr>