Problem with Dependent Dropdown

I don't know where should be written the actionDynamiccities() method. I have a form for customer, for example, in this I have country dropdown, state dropdown and city dropdown, What do I need to add to the city model &/or controller?, what in state?, and what in country?. In the form I have CHtml::activeDropDownList($customer,'laboraladdress_idcountry'… is this ok to have the $customer?, why is not in the example? Do you have a clear example with what should be added on the (user/create) view? what in the controller?, and what in the model/s?.

THanks in advance.  ;)

IMHO, CountryController, StateController and CityController should have action actionGetList (for example), which should return list of options for corresponding  dropdowns.

These actions will be called from the customer's view via select's onchange.

In case of sophisticated logic for records list (for example, a bunch of extra conditions) you also may want to add public function getList to model, and call it instead of simple findAll from controller.

Thanks Angel De La Noche, I will show you what I did cause I don't understand very well, I am trying to use a dependent dropdowns with country-state-city in a form to create customers=clientes. What I did in the clientes form is this:

In the file _form.php I have this:

Clientes/es/_form.php

The Paises dropdown renders correctly but in provincia it is showing an error message in the form that says:

(the label)Provincia

Fatal error: Call to a member function hasErrors() on a non-object in C:\xampp\htdocs\mistery1\yiiframework\framework\web\helpers\CHtml.php on line 1215

I don't know where to write and if it is correct the:

I write it in the ClientesController.php but is not working.

Where is the mistake? how can I make this dropsdawn work correctly? I didn't try to do it in ciudades(citys) yet but if you can tell me how can I implement it also it will be great.

Thanks a lot again.

I make a little advance solving my problem, what I did is in the customer _form:

In the Provincia Controller I added the method for getting the province deppending on the country/pais:

When it start I get the drop country fill with all the countries. Good. The selected country at start is the default value "Select…".Good. In the drop provincia appears the default value "Select…". Good. When I select a different country, it fills the province drop. Exelent. The selected item when province if filled with new values is not "select…" but the first province name. BAD! When I change the vale in the Province drop nothing happens with Cities drop. BAD! How can I solve the bads? please qiang a clue…

Well, this is all I could do, In the _form.php:



<div class="simple">


<?php echo CHtml::activeLabelEx($clientes,'idPaisCliente'); ?>


<?php echo CHtml::dropDownList('dirlab_idpais','', Paises::model()->getAllList(), array(


'prompt'=>'Seleccione...',          //


'value'=>'0',


'ajax' => array(


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


'url'=>'/mistery1/misteryapp/index.php?r=provincia/Dynamicprovincias', //url to call


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


//'data'=>'js:document.getElementById("pruebaprovincia").style.display="inline";',


//leave out the data key to pass all form values through


))); ?>


</div>


<div class="simple">


<?php echo CHtml::activeLabelEx($clientes,'idProvinciaCliente'); ?>


<?php echo CHtml::dropDownList('dirlab_idprovincia','', array(), array(


'prompt'=>'Seleccione...',          //


'value'=>'0',


'ajax' => array(


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


'url'=>'/mistery1/misteryapp/index.php?r=localidad/Dynamiclocalidades', //url to call


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


//'data'=>'js:alert("prueba")',


//leave out the data key to pass all form values through


))); ?>


</div>


<div class="simple">


<?php echo CHtml::activeLabelEx($clientes,'idCiudadCliente'); ?>


<?php echo CHtml::dropDownList('dirlab_idciudad','',array(), array( 'prompt'=>'Seleccione...','value'=>'0')) ?>


</div>


in the ProvinciaController:



public function actionDynamicprovincias()


    {


        $data=Provincia::model()->findAll('idpais=:idpais',


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


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


        $data[0]="Seleccione...";


        ksort($data);


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


        {


            echo CHtml::tag('option',


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


        }


    }


There are two lines that I add to show a "Select…" option in the drop cause it doesn't work seting it in the 'prompt'=>'Seleccione…', 'value'=>'0'.

Another thing I have to do is, in the ClientesController in the create method and in the update I had to modify this cause it doesn't save the provincia, pais, ciudad ids:



public function actionCreate()


	{


		$clientes=new Clientes;


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


		{


			$clientes->attributes=$_POST['Clientes'];


            $clientes["dirlab_idpais"] = $_POST['dirlab_idpais'];


            $clientes["dirlab_idprovincia"] = $_POST['dirlab_idprovincia'];


            $clientes["dirlab_idciudad"] = $_POST['dirlab_idciudad'];


			if($clientes->save())


				$this->redirect(array('show','id'=>$clientes->id));


		}


		$this->render('create',array('clientes'=>$clientes));


	}


Now it is working, saving the ids, showing the select option, but the problem is when you try to modify, how can I show this options that are saved to show it again. If this that I am doing is wrong what is the correct way. Thanks in advance.