Updating a hidden field using auto-complete in Yii2

I am working on autocomplete widgets. This is my code so far.

My opinion:

<?php $data = Company::find() ->select(['name as value', 'name as label','c_id as id']) ->asArray() ->all(); echo AutoComplete::widget([ 'name' => 'Company', 'id' => 'ddd', 'clientOptions' => [ 'source' => $data, 'autoFill'=>true, 'minLength'=>'4', 'select' => new JsExpression("function( event, ui ) { console.log(ui); $('#user-company').val(ui.item.id); }")], ]); ?> <?= Html::activeHiddenInput($model, 'company')?> 

If you select the option, the autocomplete parameters are updated in the text field, but the hidden field is not updated.

How can I get a hidden field to update when I select an option?

+4
php yii2
source share
1 answer

You can use the following properties:

  • labels - shown in the drop-down menu,
  • value - goes to the input field after selection,
  • id is an optional parameter to use the hiden field.

For example:

 <input type="hidden" id="user_company" name="user_company" value="qwe"> <?php use yii\web\JsExpression; echo AutoComplete::widget([ 'name' => 'company', 'id' => 'ddd', 'clientOptions' => [ 'source' => [ ['label'=>'color1', 'value'=>'key1', 'id'=>'c_id1'], ['label'=>'color2', 'value'=>'key2', 'id'=>'c_id2'] ], 'autoFill'=>true, 'minLength'=>'0', 'select' => new JsExpression("function( event, ui ) { console.log(ui); $('#user_company').val(ui.item.id); }") ], ]);?> 
+6
source share

All Articles