S
S
Silverviql2018-09-24 16:56:29
Yii
Silverviql, 2018-09-24 16:56:29

How to set default value in yii2 dropdownlist?

<?=   
                    $form->field($model, 'id_shop')->dropDownList(ArrayHelper::map(User::find()
                        ->selectUser()
                        ->all(),
                        'id', 'name'))->label(false) ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-09-24
@Silverviql

Here is a simple example.

$form->field($mod, 'testDrop')->dropDownList([0 => '1', 1 => '2', 2 => '3'], [
     'options' => [
             1 => ['selected' => true] // Меняя цифру можно установить какой-либо элемент массива по умолчанию
     ]
]);

In your case, I think you can try this:
$form->field($model, 'id_shop')->dropDownList(ArrayHelper::map(/*********/), [
     'options' => [
             2 => ['selected' => true] // Если, конечно, такой id есть в базе
     ]
]);

O
OkOl, 2018-09-29
@OkOl

via promt or if you need something special
<?php
$a=Categories::find()->select(['name', 'id'])->indexBy('id')->orderBy('name ')->column();
$a[0]='';
echo $form->field($model, 'parent_id')->dropdownList(
$a,
[
'options' => [
'0' => ['selected' => true]
]
]
)->label('Parent category'); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question