CJuiAutocomplete not storing ID

I am trying to make CJuiAutocomplete work but I can’t.

I am following this example (and others also…):




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

	    'name'=>'test1',

	    'value'=>'test21',

	    'source'=>$this->createUrl('jui/autocompleteTest'),

            //I guess I have problem with the js:function needed here

	    'options'=>array(

	            'showAnim'=>'fold',

	    ),

	));






public function actionAutocompleteTest() {

	    $res =array();

	 

	    if (isset($_GET['term'])) {

	        $qtxt ="SELECT username FROM {{user}} WHERE username LIKE :username";

	        $command =Yii::app()->db->createCommand($qtxt);

	        $command->bindValue(":username", '%'.$_GET['term'].'%', PDO::PARAM_STR);

	        $res =$command->queryColumn();

	    }

	 

	    echo CJSON::encode($res);

	    Yii::app()->end();

	}



It is not storing the ID in db, " …x_ID cannot be blank."

Can anyone help with an example?

  1. I also want to achive a multiselect autocomplete, eg. registering many participants in a training…can anyone suggest a solution?

Thank you in advance.

Does anyone have any working solution in your projects for CJuiAutocomplete?

Please can you share here your working code? Thank you…

Whatever example/wiki I follow I get the error that "xyz ID cannot be blank".

Autocomplete working, item selected, but when form is submitted the ID is blank…

I guess you just don’t provide the ID to be saved there, am I right? Available list of user names is just showing the name itself and nothing else and you want the ID (of the user?) to be saved after selecting.

Take a look here http://api.jqueryui.com/autocomplete/#option-source - you can prepare data source to give label and value all together so label would be user name and value would be user id.

If you are using Bootstrap in your project you could use AjaxDropDown extension to get filtered list of names to select from.

Thank you Bizley for your answer (I am not using Bootstrap).

Here is the working code for me:

_form.php:




<div class="row">

	<?php echo $form->labelEx($model,'cTeilnehmerID'); ?>

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

				    'name'=>'cNachname',

				    //'value'=>'',

				    'sourceUrl'=>array('teilnehmer/autoComplete'),

				    // additional javascript options for the autocomplete plugin

				    'options'=>array(

					'minLength'=>'1',

				        'showAnim'=>'fold',

					'select'=>'js:function(event, ui){

						$("#'.CHtml::activeId($model, 'cTeilnehmerID').'").val(ui.item.id);

					}'

				    ),

				    'htmlOptions'=>array(

					'style'=>'height:15px;width:150px;',

				    ),

				));

		?>

		<?php echo $form->hiddenField($model, 'cTeilnehmerID'); ?>

		<?php echo $form->error($model,'cTeilnehmerID'); ?>

	</div>



and controller action:




public function actionAutoComplete() {

		

	$criteria = new CDbCriteria;

        $criteria->select = array('cTeilnehmerID','cVorname','cNachname');


        $criteria->addSearchCondition('cVorname',  strtoupper( $_GET['term']) ) ;

	$criteria->addSearchCondition('cNachname', strtoupper ( $_GET['term']), true, 'or');

        $criteria->limit = 15;

        $data = Teilnehmer::model()->findAll($criteria);


        $res = array();

        

        foreach ($data as $item) {

                        

            $res[] = array(

                'id' => $item->cTeilnehmerID,

                'value' => $item->cVorname." ".$item->cNachname,

                'label' => $item->cVorname." ".$item->cNachname,

                                

            );

                        

        }


        echo CJSON::encode($res);

}



There was a problem when trying to update one record, the field was empty.

So to correct this (the value should be there if the record was created and now trying to update):

the _form.php should be:





<div class="row">

	<?php echo $form->labelEx($model,'cTeilnehmerID'); ?>

	<?php 

	if(isset($model->cTeilnehmerID)){

		$var=intval($model->cTeilnehmerID);

		$criteria = new CDbCriteria;

		$criteria->select = array('cTeilnehmerID','cVorname','cNachname');

	

		$criteria->addSearchCondition('cTeilnehmerID', $var);

		$data = Teilnehmer::model()->find($criteria);

	

		$value = $data->cVorname." ".$data->cNachname;


	}

	else {$value='';}

		

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

			    'name'=>'fullName',

			    'value'=>$value, //so if the record is already created the value will be shown here

			    'sourceUrl'=>array('buchung/autoComplete'),

			    // additional javascript options for the autocomplete plugin

			    'options'=>array(

					'minLength'=>'1',

				        'showAnim'=>'fold',

					'select'=>'js:function(event, ui){

						$("#'.CHtml::activeId($model, 'cTeilnehmerID').'").val(ui.item.id);

						}'

			    ),

	    		   'htmlOptions'=>array(

					'style'=>'height:15px;width:150px;',

					),

				));

	?>

	<?php echo $form->hiddenField($model, 'cTeilnehmerID'); ?>

	<?php echo $form->error($model,'cTeilnehmerID'); ?>

</div>