Autocomplete - save id instead of value in hidden form

Hi everyone,

I’m having problem trying to save Place.id into $model->placeID.

I’ve created a field that will help the user to autocomplete the names of the place (place->name) but I would like to store the corresponding place id (place->id) into the database instead.

The autocomplete works but I’m unable to retrieve the place.id and store it into the database.

Please help. Thanks.





use yii\web\JsExpression;

use yii\jui\AutoComplete;




$data = Place::find()

    ->select(['name as value', 'name as  label','id as id'])

    ->asArray()

    ->all();


?>

<?php echo $form->field($model,'placeID')->widget(\yii\jui\AutoComplete::classname(), [

  	'clientOptions' => [

         'source' => $data, 

        'minLength'=>'2', 

    'autoFill'=>true,

         'select' => new JsExpression("function( event, ui ) {

		  $('#placename').val(ui.item.id);

     }")],

     ]);

	 

	?>




I also tried the following code without success.

The autocomplete works but I can’t extract the corresponding place.id.

What could be the problem?




<?php

	

    $data = Place::find()

    ->select(['name as value', 'name as  label','id as id'])

    ->asArray()

    ->all();

	

   echo AutoComplete::widget([

    'name' => 'placeID',

    'id' => 'ddd',

    'clientOptions' => [

      'source' => $data,

      'autoFill'=>true,

      'minLength'=>'2',

      'select' => new JsExpression("function( event, ui ) {

        

        $('#placeID').val(ui.item.id);

      }")




    ],


  ]);?>

	

	<?= Html::activeHiddenInput($model, 'placeID')?>




Autocomplete works but I still can’t $model->placeID get the corresponding id. Please help.


	


<?php	

    $data = Place::find()

    ->select(['name as value', 'name as  label','id as id'])

    ->asArray()

    ->all();




    echo 'Place' .'<br>';

    echo AutoComplete::widget([    

    'clientOptions' => [

    'source' => $data,

    'minLength'=>'3', 

    'autoFill'=>true,

    'select' => new JsExpression("function( event, ui ) {

                    $('#model-placeID').val(ui.item.id);

                 }")],

                 ]);


	 

	?>

    <?= Html::activeHiddenInput($model, 'placeID')?>

Hi guys.

I managed to solved the problem.

The issue is


'select' => new JsExpression("function( event, ui ) {$('#photo-placeid').val(ui.item.id);}")],]);

Need to name the #photo-placeid correctly.

"photo" is the name of the form.

"placeid" is the name of the field. Must be in lowercase.

View the page source if you are uncertain. My page source appears as


<input type="hidden" id="photo-placeid" name="Photo[placeID]">	

The full code that works for me is as below.





<?php


<div class="photo-form">


.....some other codes

	

    $data = Place::find()

    ->select(['name as value', 'name as  label','id as id'])

    ->asArray()

    ->all();


    echo 'Place' .'<br>';

    echo AutoComplete::widget([    

    'clientOptions' => [

    'source' => $data,

    'minLength'=>'2', 

    'autoFill'=>true,

    'select' => new JsExpression("function( event, ui ) {

                    $('#photo-placeid').val(ui.item.id);

                 }")],

                 ]);

	?>

	 <?= Html::activeHiddenInput($model, 'placeID')?>



Thanks a lot for providing the solution !