I am using the CAutoComplete widget to lookup people in a "users" table based on a LIKE match on the users' names. However, once a person has been selected from the users table with the CAutoComplete box, I want to submit the form and have the database ID of the user posted back to the server, not just the person's name. (The name is only displayed for usability…the ID is what is really used by the code to find the user record and return associated data.) Is there a good way to do this? Any ideas are much appreciated. Thanks!
Thanks for pointing me in the right direction, Qiang. For those interested, here's what I did to make this work:
Since I am calling an action to return my dataset, I use Yii to query values for autocomplete population. In order to "return" multiple values, you have to split up your values using the pipe "|" delimiter. Here's sample code:
public function actionAutoPopulateLookup()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
$name = $_GET['q'];
$limit = $_GET['limit'];
$criteria = new CDbCriteria;
$criteria->condition = "name LIKE :sterm";
$criteria->params = array(":sterm"=>"%$name%");
$criteria->limit = $limit;
$returnVal = '';
$userArray = User::model()->findAll($criteria);
foreach($userArray as $userAccount)
{
$returnVal .= $userAccount->getAttribute('name').'|'.$userAccount->getAttribute('user_id')."n";
}
echo $returnVal;
}
die();
}
Once your php is returning pipe-delimited strings terminated by "\n", you can now use the associated values with your autocomplete widget and a hidden field, like so:
Notice the methodChain attribute being used. This appends (or chains) a javascript method to the end of the js AutoComplete widget. This particular method is the "result" method, which references the hidden field and assigns the 2nd part (part after the pipe) of the selected autocomplete data to that field.