dependent dropdown list help

Hi,

I am creating a dependent dropdown list

with two dropdown lists

One is the "group" and the other is "race"

already searched in the forum and found several cases, but still did not work with all the examples

I’ll post my code:

"_View" file code:


 

<div class="row">

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

        <?php echo $form->dropDownList($model,'grupo_id',CHtml::listData(Grupo::model()->findAll(),'grupo_id','grupo_nome'),

                        array(

                        	'prompt'=>'Selecione um grupo',

                        	'ajax'=> array(	

                        	'type' => 'POST',

	                        'url'=>CController::createUrl('Pet/DynamicRacas'),

	                        'update'=>'#'.CHtml::activeId($model,'raca_id'), 

                        	)    

                        )   

                    );

        ?>

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

        </div>

        <div class="row">

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

            <?php echo $form->DropDownList($model,'raca_id',array()); ?>

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

        </div>



Controller code:




public function actionDynamicRacas()

    {                    

        $data = Raca::model()->findAll('grupo_id=:grupo_id',

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


        $data = CHtml::listData($data,'raca_id','raca_nome');

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

            {

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

            }

    }	



the code generated:




<div class="row">

        <label for="Pet_grupo_id" class="required">Grupo <span class="required">*</span></label> 

        <select name="Pet[grupo_id]" id="Pet_grupo_id">

           <option value="">Selecione um grupo</option>

           <option value="2">Gato</option>

           <option value="1">Cao</option>

         </select>               

</div>

<div class="row">

            <label for="Pet_raca_id" class="required">Raça <span class="required">*</span></label> 

            <select name="Pet[raca_id]" id="Pet_raca_id">

            </select>            

</div>



The second drop down list does not receive anything.

How do I check if the function dynamic races reaches to be accessed?

I solved the problem

in the controler i change this:


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

for this:


 array(':grupo_id'=>(int) $_POST['Pet']['grupo_id'])); 

I had already tried this

I dont know why it did not work before, but now work

I leave here the code if someone needs:

view file:




<div class="row">

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

        <?php echo $form->dropDownList($model,'grupo_id',CHtml::listData(Grupo::model()->findAll(),'grupo_id','grupo_nome'),

                        array(

                        	'prompt'=>'Selecione um grupo',

                        	'ajax'=> array(	

                        	'type' => 'POST',

	                        'url'=>CController::createUrl('Pet/DynamicRacas'),

	                        'update'=>'#'.CHtml::activeId($model,'raca_id'), 

                        	)    

                        )   

                    );

        ?>

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

	</div>

	<div class="row">

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

		<?php echo $form->DropDownList($model,'raca_id',array()); ?>

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

	</div>



controller file:




public function actionDynamicRacas()

    {                    

        $data = Raca::model()->findAll('grupo_id=:grupo_id',

                        array(':grupo_id'=>(int) $_POST['Pet']['grupo_id']));

        $data = CHtml::listData($data,'raca_id','raca_nome');

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

            {

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

            }

    }	



and in the controller file, is needed to add the permissions:




public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('DynamicRacas','create'),

				'users'=>array('*'),

			    )

                           );

	}



ok, now I have a question

does anyone know how to get when open the page, the values ​​for the second drop menu,

the 1st menu appears the first option by default

for example: "dog"

and in the 2nd menu I want to appear the options related

I guess you have to replace this line:


 <?php echo $form->DropDownList($model,'raca_id',array()); ?> 

with this one:


 <?php echo $form->DropDownList($model,'raca_id',CHtml::listData(Raca::model()->findAll(),'raca_id','raca_nomo')); ?> 

I think you need to find and show only the valid items for grupo_id so you need to modify with something like:




<?php echo $form->DropDownList($model,'raca_id',CHtml::listData(Raca::model()->findAllByAttributes(array('grupo_id'=>$model->grupo_id)),'raca_id','raca_nomo')); ?>