Using CAutoComplete to display one value and submit another

Comparing #1 with #2

Revision #2 was created by dalip dalip on Mar 25, 2009, 8:40:46 AM.

syntax highlight

Content

[...]
While not all features and options are explored, here is a sample of how to implement the above steps.

In your controller, create a new action to handle autocomplete lookup queries, like so:

     
```php 
<?php
//...
public function actionAutoCompleteLookup()
[...]
//...
?>
```
There are a few things to notice here:
[...]
Now, in your view file, embed the following code within your form:

 
    
 
```php 
<?php echo $this->widget('CAutoComplete',
array(
'name'=>'user_name', //name of the html field that will be generated
[...]
?>
<?php echo CHtml::hiddenField('user_id'); ?>
``` Take note of the `methodChain` attribute being used. MethodChain essentially appends (or chains) a javascript method to the end of the AutoComplete javascript code that will be generated. This particular method is the `result` method, which fires when an autocomplete item is selected. The code inside of the result function basically references the hidden field that was defined and assigns the 2nd part (part after the pipe) of the selected autocomplete data to that field. Now that the basics are out of the way, review the documentation for [Yii's CAutoComplete widget](http://www.yiiframework.com/doc/api/CAutoComplete) and [jQuery's Autocomplete plugin](http://plugins.jquery.com/project/jq-autocomplete) to find more ways to customize and use autocomplete functionality in your Yii project.