How to make a drop down list in yii2

Yii2 DropDownList syntax

echo $form->field($model, 'name_field')->dropDownList(
    [items],
    [options]
);

DropDownList with model data

Using below code snippet you can display the database table column’s data in the dropdownList via modal.

//use app\models\Country;
$countries=Country::find()->all();

//use yii\helpers\ArrayHelper;
$listData=ArrayHelper::map($countries,'code','name');

echo $form->field($model, 'name')->dropDownList(
    $listData,
    ['prompt'=>'Select...']
);

 

Add a Comment

Your email address will not be published. Required fields are marked *