user2211486 user2211486 - 3 months ago 41
PHP Question

How to add default value selected in dropdownlist with enum?

I am new to yii2 and trying to get around. I have a dropdownlist whose values in the database are enum. So when the crud was created the dropdownlist had the enum values.

But I want to keep one value selected as default in the drop down list.
My form code is below:

<?= $form->field($model, 'priotiy_level')->dropDownList([ 'low' => 'Low', 'medium' => 'Medium', 'high' => 'High', ], ['prompt' => 'Select Priority Level']) ?>


Instead of the prompt, I want to have medium as a selected value. Can someone please help me with this?

Thank you.

Answer

After initialization of the $model instance in your controller set the attribute and then pass $model to view.

$model->priority_level = 'medium';
Comments