dependent dropdowns not working with activeDropDownList

Hi,

I am new to YII. I am unable to get dependent drop downs work with CHTML::activeDropDownList. When the element name has [] in the name AJAX requests are not being made.

i.e <select id="Employee_customer" name="Employee[customer]">

If I use CHTML::dropDownList instead as per cookbook http://www.yiiframework.com/wiki/24/

I get validation error that field cannot be blank. This is happening because

Below the code in _form.php

<div class="row">

	&lt;?php echo CHtml::activeLabelEx(&#036;model,'customer'); 


		&#036;custOptns=Customer::model()-&gt;fetchCustomers();


	?&gt;


	&lt;?php echo CHtml::activeDropDownList(&#036;model,'customer', &#036;custOptns, array('prompt'=&gt;'Please Select','ajax'=&gt;array('type'=&gt;'POST','url'=&gt;CController::createUrl('customerManager/fetchcustmgr'),'data'=&gt;'js:jQuery(this).serialize()','update'=&gt;'#Employee_project_manager')));


	?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'customer'); ?&gt;


&lt;/div&gt;








&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo CHtml::activeLabelEx(&#036;model,'project_manager'); ?&gt;


	&lt;?php echo CHtml::activeDropDownList(&#036;model,'project_manager',array()); 


		


	?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'project_manager'); ?&gt;


&lt;/div&gt;

In Controller

public function actionFetchcustmgr()

{


	


	&#036;data=CustomerManager::model()-&gt;findAll('tbl_customer_idcustomer=:id_cust',array(':id_cust'=&gt;&#036;_POST['Employee_customer']));


	Yii::log(&quot;Value--&#62;&quot;.&#036;_POST['Employee_customer'], &quot;info&quot;, &quot;application.controllers.CustomerManagerController&quot;);





	 		


	&#036;data=CHtml::listData(&#036;data,'idcustomer_mgr','first_name');


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


	{


    	


		Yii::log(CHtml::tag('option',array('value'=&gt;&#036;value),CHtml::encode(&#036;name),true), &quot;info&quot;, &quot;application.controllers.CustomerManagerController&quot;);


		


		echo CHtml::tag('option',


            array('value'=&gt;&#036;value),CHtml::encode(&#036;name),true);


	}


}

Anybody please help.

Basically, it is missing the ‘id’ attribute




<?php echo CHtml::activeDropDownList($model,'project_manager',array(), array('id' => 'Employee_project_manager' )); 

			

@vnagar

Note that when inserting code you can use the code directives so that your code is more readable… use [code ] inser your code here[/code ]… without spaces…

or the <> button in the toolbar

Thanks for the suggestion. I will use code directives.

Thanks for your reply.

When CHtml::activeDropDownList is used I see that it automatically creates the id & name attribute for the select element using model attribute. I have tried adding id attribute & name attribute but it does not work.

In the first place the 1st dropdown does not trigger ajax request when activeDropDownList is used. In the controller function actionFetchcustmgr() I have put a log statement. When I select a item in the dropdown it gets logged but I see nothing happens when activedropdownlist is used.

Everything works fine if I use dropdownlist. But I cannot submit the form since the validator throws value cannot be blank because it is expecting Employee[customer] to be the name of the select element.

Thanks,

i have the same problem,ajax doesnt triggs

Same problem here… Ajax doesn’t trigger

If anyone knows the solution, please post.

Can you post your non-working code?

Have you tried to debug (eg with Firebug) to see the generated jQuery code, and see whether it’s ever reached?

I’ve posted this on another forum (don’t want to be accused of spamming the boards, but here it is:

The _form code in question:




echo $form->dropDownList(

                        $model,

                        'CommodityCode',

                        array(

                            CHtml::listData(Commodity::model()->findAll(),'CommodityCode','CommodityName'),

                            array(

                                'ajax' => array(

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

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

                                    'update'=>'CoverageLine_PracticeCode', //selector to update

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

                                    )

                                )

                            )

                        ); 

                        



The Header of that form:




$form=$this->beginWidget('CActiveForm', array(

	'id'=>'coverageline-form',

        'enableClientValidation'=>false,

	'enableAjaxValidation'=>true,

));



The calling controller function:




	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation($model);


		if(isset($_POST['Coverageline']))

		{

			$model->attributes=$_POST['Coverageline'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->CoverageLineId));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}



The Called Controller function:




        public function actionGetPractice()

        {

            

            $data=Practice::model()->findAll('CommodityCode=:CommodityCode', 

                  array(':CommodityCode'=>$_POST['CoverageLine']['CommodityCode']));

                  

            $data=CHtml::listData($data,'PracticeCode','PracticeName');

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

            {

                echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($name),true);

            }

        }




Hi,

I’d like to up this topic, because i’m still stuck whatever trick or tuts I could find on the web.

Do someone have some kind of a clue?

Thank you,

Patrick