Yii2 Kartik-V Typeahead Basic autocomplete by name but retains integer value

Updates have been made below.

I am trying to use the Kartik-V Typeahead Basic main widget with the Yii2 Framework.

The code below works to display the necessary data, the user can search by the name of the university and appears in the autocomplete list.

The problem is that the model requires a university identifier, not a name. Thus, the rules of this field can only store an integer and return a validation error as soon as you select one of the typeahead results.

<?= $form->field($model, 'university_id')->widget(TypeaheadBasic::classname(), [
    'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
    'pluginOptions' => ['highlight' => true],
    'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>

I hope someone can help me figure out if there is a parameter that needs to be changed, so when saving, the user data 'uni_name' changes to uni 'id' again.

UPDATE: , "Insane Skull".

:

<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
    'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
    'pluginOptions' => ['highlight' => true],
    'options' => ['placeholder' => 'Filter as you type ...', 'id' => 'testID'],
    'pluginEvents' => [
        'typeahead:select' => new yii\web\JsExpression("function(event, ui) { $('#testing123').val(ui.item.id); }"),
    ]
]); ?>

<?= Html::activeHiddenInput($model, 'university_id', array ('id' => 'testing123'))?>

, , : yii\web\JsExpression:: __ toString()

+4
2

Select2 Typeahead, , Select2, Typeahead.

<?= $form->field($model, 'university_id')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
    'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>
+4

activeHiddenInput() .

, name.

:

<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...'],
'select' => new yii\web\JsExpression("function( event, ui ) {
                        $('#id_of_hiddenField').val(ui.item.id);
                    }")
]); ?>
<?= Html::activeHiddenInput($model, 'university_id')?>

activeHiddenField.

0

Source: https://habr.com/ru/post/1607956/


All Articles