I had faced a problem in CAutoComplete, during displaying of related ids of the user names in text field.
As when we type multiple user name in the text box, names are displayed in comma separated form (eg. Johna, Robin, Humble). But it’s ids are replaced by current one in the hidden field.
Few twist had been made to resolve the problem.
The original code is:
<?php $this->widget('CAutoComplete',
array(
//name of the html field that will be generated
'name'=>'user_name',
//replace controller/action with real ids
'url'=>array('controller/action'),
'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'=>'40'),
[b] 'methodChain'=>".result(function(event,item){\$(\"#user_id\").val(item[1]);})",[/b]
));
?>
<?php echo CHtml::hiddenField('user_id'); ?>
Change has to be made in bold highlighted line:
[b]'methodChain'=>".result(function(event,item){\$(\"#user_id\").val(item[1]+','+\$(\"#user_id\").val());})",[/b]
And for security reason make hidden form as read only. If we don’t assign it as read only, we can insert any text inside the hidden field.
[b]<?php echo CHtml::hiddenField('user_id','', array('readonly'=>'readonly')); ?> [/b]