CJuiAutoComplete problem

Hi all,

I have a problem with CJuiAutoComplete, my autoComplete don´t work.

I have 2 models:

1 - Countries (Country model)



2 - Regions (Region model)




and this code on ‘/views/regions/_form.php’:

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

		'name' => 'country_id',



	         // additional javascript options for the autocomplete plugin








And this on ‘/controlers/CountriesControler.php’:


         * @param string $term The string to match (automatically sent by the cjuiautocomplete widget)

         * @param int $limit  


        public function actionAutocomplete( $term, $limit = 50 )


                // build the query criteria

                $countryCriteria = new CDbCriteria();

                $countryCriteria->condition = "name LIKE '$term%'";

                $countryCriteria->order = "name ASC";

                $countryCriteria->limit = $limit;

                // get the possible options

                $countries = Country::model()->findAll( $countryCriteria );

                foreach( $countries as $country )


                        $returnVal[] = array('label'=>$country->name,'id'=>$country->id);


                // output as json for the autocomplete to render

                echo CJSON::encode($returnVal);


I don´t know what is the problem, but its happend all ok. but the AutoComplete don´t work.

First of all, welcome to this forum! :D

Several things that you may want to check:

  1. Has your autocomplete action been added to access rules?

  2. Is your json output correct?

  3. We don’t need to add $term parameter to the function. We can get it from $_GET[‘term’].

Let us know if you still get problems on these. :)

Thanks, rei !

I checked with Firebug from Firefox and it´s happend the problem is on access rules.

Im new on yii, and i´m going to try to resolve, but i don´t know exactly how.

Hello Javier

your call to CWebApplication::createUrl() is incorrect. It only requires the ID of your controller and action.

As you state your controller is called CountriesController the ID of it resolves to ‘countries’ and your Action actionAutocomplete resolves to ‘autocomplete’.

Therefore your call should look like:


Also: The way how u use the $term variable is making your Application vulnerable to SQL Injection.

You might want to use a statement like this

[PHP]$countries = Country::model()->findAll(‘name=:name’, array(’:name’ => $term));[/PHP]

// Edit:

Excuse me, my SQL fix is not appropriate for a LIKE Statement. I guess the following code should do it:


$dbCriteria = new CDbCriteria(array(‘order’ => ‘name’));

$dbCriteria->addSearchCondition(‘name’, $term);

$countries = Country::model()->findAll($dbCriteria);


You can add autocomplete action to accessRules() function in your controller (CountriesController.php).

Also, Coksnuss made valid points on createUrl and SQL injection. I missed that. :)