P
P
Petr Fronin2016-04-01 17:21:18
Yii
Petr Fronin, 2016-04-01 17:21:18

Yii2 AutoComplete how to insert into the field not id but name?

I use in Yii2 AutoComplete:

echo $form->field($model, 'city')->widget(AutoComplete::classname(), [
        'clientOptions' => [
            'source' => City::find()->select(['city_id as value', 'name as label'])->asArray()->all()
        ],
        'options'=>[
            'placeholder' => 'Ваш город (начните вводить)',
            'class' => 'form-control form-control-signup input-sm select-registration'
        ]
    ])->label('');

and this problem arises with it:
you start entering the name of the city - possible city options appear - you select the required city and its id is inserted into the input field, but I would like name!
How is it possible to implement it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DuD, 2016-04-01
@DuD

Try 2 options:
1) source' => City::find()->select(['name as label'])->asArray()->all()
2) source' => City::find() ->select(['name as value', 'name as label'])->asArray()->all()

M
Maxim Timofeev, 2016-04-03
@webinar

Instead of
should

ArrayHelper:map(City::find()->select(['city_id', 'name'])->all(),'name','name');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question