CAutoComplete two dependent fields

hi, i want to have two autocomplete fields in the same form, The second field should depend on first field’s value.

i tried to do this:

view:


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

        <?php $this->widget('CAutoComplete',

          array(

                         //name of the html field that will be generated

             'name'=>'city_name', 

                         //replace controller/action with real ids

             'url'=>array('cityLookup'), 

             'max'=>10, //specifies the max number of items to display

 

                         //specifies the number of chars that must be entered 

                         //before autocomplete initiates a lookup

             'minChars'=>2, 

             'delay'=>500, //number of milliseconds before lookup occurs

             'matchCase'=>false, //match case when performing a lookup?

 

                         //any additional html attributes that go inside of 

                         //the input field can be defined here

             'htmlOptions'=>array('size'=>'25'), 

 

             'methodChain'=>".result(function(event,item){\$(\"#city_id\").val(item[1]);})",

             ));

             //echo Yii::trace(CVarDumper::dumpAsString(),'vardump'); 

    ?>


 

    <?php echo CHtml::hiddenField('city_id'); ?>

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

 

    </div>

 

    <div class="row">

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

        <?php $this->widget('CAutoComplete',

          array(

                         //name of the html field that will be generated

             'name'=>'street_name', 

                         //replace controller/action with real ids

             'url'=>array('streetLookup'), 

             'max'=>10, //specifies the max number of items to display

 

                         //specifies the number of chars that must be entered 

                         //before autocomplete initiates a lookup

             'minChars'=>2, 

             'delay'=>50, //number of milliseconds before lookup occurs

             'matchCase'=>false, //match case when performing a lookup?

 

                         //any additional html attributes that go inside of 

                         //the input field can be defined here

             'htmlOptions'=>array('size'=>'25'), 

 

             'methodChain'=>".result(function(event,item){\$(\"#street_id\").val(item[1]);})",

             ));

             //echo Yii::trace(CVarDumper::dumpAsString(),'vardump'); 

    ?>

 

    <?php echo CHtml::hiddenField('street_id'); ?>

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

    </div>

first field controller action:


public function actionCityLookup()

    {

 

       if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))

       {

            /* q is the default GET variable name that is used by

            / the autocomplete widget to pass in user input

            */

          $name = $_GET['q'];

 

                    // this was set with the "max" attribute of the CAutoComplete widget

          $limit = min($_GET['limit'], 50); 

          $criteria = new CDbCriteria;

          $criteria->condition = "name LIKE :sterm";

          $criteria->params = array(":sterm"=>"%$name%");

          $criteria->limit = $limit;

          $cityArray = City::model()->findAll($criteria);

          $returnVal = '';

          foreach($cityArray as $city)

          {

             $returnVal .= $city->getAttribute('name').'|'

                                         .$city->getAttribute('id')."\n";

          }

 

          echo $returnVal;

       }

    }

second field conroller action:


public function actionStreetLookup()

    {

 

       if(Yii::app()->request->isAjaxRequest && isset($_GET['q']) && isset($_POST['city_id']))

       {

            /* q is the default GET variable name that is used by

            / the autocomplete widget to pass in user input

            */

          $name = $_GET['q'];

          $city = $_POST['city_id'];

 

                    // this was set with the "max" attribute of the CAutoComplete widget

          $limit = min($_GET['limit'], 50); 

          $criteria = new CDbCriteria;

          $criteria->condition = "name LIKE :sterm AND city_id:=cityId";

          $criteria->params = array(":sterm"=>"%$name%",":cityId"=>$city);

          $criteria->limit = $limit;

          $streetsArray = Street::model()->findAll($criteria);

          $returnVal = '';

          foreach($streetsArray as $street)

          {

             $returnVal .= $street->getAttribute('name').'|'

                                         .$street->getAttribute('id')."\n";

          }

 

          echo $returnVal;

       }

    }

the $_POST['city_id] didn’t work…

please help.

[Solved]

i’ve added the extraParams parameter to the street autocomplete like this:




'extraParams'=>array('city'=>"js:function(){return $(\"#city_id\").val();}"),



and altered the controller action like this:




public function actionStreetLookup()

    {

    	

       if(Yii::app()->request->isAjaxRequest && isset($_GET['q']) && isset($_GET['city']))

       {

            /* q is the default GET variable name that is used by

            / the autocomplete widget to pass in user input

            */

          $name = $_GET['q'];

          $city = $_GET['city'];

          

                    // this was set with the "max" attribute of the CAutoComplete widget

          $limit = min($_GET['limit'], 50); 

          $criteria = new CDbCriteria;

          $criteria->condition = "name LIKE :sterm AND city_id=:cityId";

          $criteria->params = array(":sterm"=>"%$name%",":cityId"=>$city);

          $criteria->limit = $limit;

          $streetsArray = Street::model()->findAll($criteria);

          $returnVal = '';

          foreach($streetsArray as $street)

          {

             $returnVal .= $street->getAttribute('name').'|'

                                         .$street->getAttribute('id')."\n";

          }

          

          echo $returnVal;

       }

    }