I am using a CAutoComplete to aid in filtering a list of Players. The return shows Player.first_name, Player.second_name, Player.dob, Player.parent_name, Player.id in order to make sure the correct player is selected (to eliminate same name confusion). Is there a way to set the Player.id returned to a hidden field or for the id to be the value returned by the CAutoComplete field? I tried to follow the cookbook thinking that the hiddenfield would be filled by JS specified in the methodChain but I am not getting the desired result so I think I may not follow the example completely.
This is my complete form, I want to link current player to existing player (player_id_1, player_id_2 in db)
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'playerlink-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($playerLinkModel); ?>
<?php echo $form->hiddenField($playerLinkModel,'season_id',array('value'=>SeasonLeague::model()->getCurrentSeasonID()));?>
<?php echo $form->hiddenField($playerLinkModel,'player_id_1',array('value'=>$playerData->id));?>
<div class="row">
<?php echo $form->labelEx($playerLinkModel,'Link To'); ?>
<?php //echo $form->dropDownList($playerLinkModel,'player_id_2', CHtml::listData(Player::model()->findAll(),'id','concatened')); ?>
<?php
$this->widget('CAutoComplete',
array(
//name of the html field that will be generated
'name'=>'player_link',
//replace controller/action with real ids
'url'=>array('currentlinks/autoCompleteLookup'),
'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'),
'methodChain'=>".result(function(event,item){\$(\"#player_id_2\").val(item[4]);})",
));
?>
<?php CHtml::hiddenField('player_id_2');?>
<?php echo $form->error($playerLinkModel,'Link To'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($playerLinkModel,'Link Type'); ?>
<?php echo $form->dropDownList($playerLinkModel,'type', array('Guaranteed'=>'Guaranteed','Sibling'=>'Sibling','Friend'=>'Friend',)); ?>
<?php echo $form->error($playerLinkModel,'Link Type'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($playerLinkModel->isNewRecord ? 'Add' : 'Add'); ?>
</div>
<?php $this->endWidget(); ?>
From my controller (which needs some some refactoring but trying to get a proof of concept working):
public function actionAutoCompleteLookup()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
/* q is the default GET variable name that is used by
/ the autocomplete widget to pass in user input
*/
$name = $_GET['q'];
// this was set with the "max" attribute of the CAutoComplete widget
$limit = min($_GET['limit'], 50);
$criteria = new CDbCriteria;
$criteria->condition = "last_name LIKE :sterm OR first_name LIKE :sterm";
$criteria->params = array(":sterm"=>"%$name%");
$criteria->limit = $limit;
$userArray = Player::model()->findAll($criteria);
$returnVal = '';
foreach($userArray as $userAccount)
{
$returnVal .= $userAccount->getAttribute('first_name').' '
.$userAccount->getAttribute('last_name').' '
.$userAccount->getAttribute('dob').' '
.$userAccount->getAttribute('parent_name').' '
.$userAccount->getAttribute('id')."\n";
}
echo $returnVal;
}
}
When I do a print of the post data I get the following:
Array ( [PlayerLink] => Array ( [season_id] => 3 [player_id_1] => 6 [type] => Guaranteed ) [player_link] => John Doe 0000-00-00 Jim and Jane Doe 1 [yt0] => Add )
I am looking for the additional hiddenfield to be present with value 1 for John Doe