AutoComplete widget. Source vs SourceUrl option

Not sure if this is a bug but when I used the AutoComplete widget I found a difference between source and sourceUrl property

I was feeding source with an array let’s say


array("aaa","bbb","ccc")

and all worked well. I was typing "aa" inside text box and I the word "aaa" would appear, in fact only the word "aaa"

On the other hand I would like to use the sourceUrl property, so I created an action inside the appropriate controller and I fed the source url property with an


array("controller/action","param"=>$param)

This action is NOT rendering any view, it just uses the


print json_encode($array);

to produce the results.

Using the second method, when I type "aa" or any other character inside the textbox of the autocomplete widget I get ALL the possible answers, meaning I will see "aaa", "bbb", "ccc"

Of course this is unwanted behaviour :)

Maybe I have misunderstood something about this widget, not sure. Thank you!

Nothing on that yet?..

If anyone happens to see this post could you please at least verify that you experience same behaviour? (notice I still use yii version 1.1.7)

Which autocomplete widget are you using? Deprecated CAutoComplete or current CJuiAutocomplete?

No it is NOT the deprecated widget

This is a sample of my code:




<?php

			$attribute = "some_attribute_name";

			print $form->labelEx($model, $attribute);

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

				"model" => $model,

				"attribute" => $attribute,

	            "source"=> $model::getNames($param),

	 //this option is buggy    //"sourceUrl"=> array("some_controller/some_actions","param" => $param),	//http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete#sourceUrl-detail

	            // additional javascript options for the autocomplete plugin

	            'options'=>array(   //options reference: http://jqueryui.com/demos/autocomplete/

	                'minLength'=>'2',

	                'delay'=>'500',

	            ),

	            'htmlOptions'=>array(

	                'style'=>'height:20px;'

	            ),

	        ));

			print $form->error($model, $attribute);

		?>



Only source or sourceUrl must be defined. If I use sourceUrl I experience the behaviour I posted from the beginning

Thank you!

Pligor is right, there is bug, if we use associative array in source attribute of CJuiAutoComplete widget(for example list generated by CHtml::listData(), or any other array whose keys do not belong to range(0,n-1), where n is number of elements).

Acceptable types for Autocomplete source are strings, arrays, and callback functions, and we are providing object here(in case of array whose keys do not belong to range(0,n-1), where n is number of elements in array),

check: zii/widgets/CJuiAutocomplete.php line 90 and helpers/CJavaScript.php line 83.

@Pligor:

Fast solution for your problem:




 "source"=> array_merge(array(),$model::getNames($param)),



So, keys will start from 0, and it will works. JQuery autocomplete do not use source array keys anyway, so you haven’t lost anything.

But I think this should be fixed in core, in CJuiAutocomplete.php file, in one of further versions.

I was wrong, autocomplete source allow json objects, but they must be in format:




var sourceObjects = [

{label:'username1', value: '1'},

{label:'username3', value: '4'},

{label:'username4', value: '5'},

]