Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 dropDownList Default value

In yii2 I have a dropdownlist:

<?= $form->field($model, 'Körperschaft')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'])?>

How can I set 'Item B' as a default value?

like image 561
yii2__ Avatar asked Dec 18 '22 03:12

yii2__


2 Answers

try this

<?= $form->field($model, 'Körperschaft')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'], ['options'=>['b'=>['Selected'=>true]]])?>
like image 105
rakhmatov Avatar answered Feb 03 '23 09:02

rakhmatov


I got it! The solutions is to write in the controller:

public function actionCreate()
{


    $model->Körperschaft='b';
like image 21
yii2__ Avatar answered Feb 03 '23 11:02

yii2__