In this tutorial, I would like to show you making the dropdown list in yii2 using different data sources like static array data and database table modal data.
First I would like to share basic syntax – here you go…
Yii2 DropDownList syntax – active field style
Yii2 DropDownList syntax – active form style
Both style are almost same , but active field style is good because it maintains the creation of the form input label and help-block for error messages where active form style generate only dropdown.
DropDownList with array data
DropDownList with model data
Using below code snippet you can display the database table column’s data in the dropdownList via modal.
Here we used
use yii\helpers\ArrayHelper. ArrayHelper has many use full functions which could be used to process arrays.
map() is the one, we are going to use here this function which will help to make a map ( of key-value pairs) from a multidimensional array or an array of objects.
Generating dropdownlist with option groups.
Below is the simple example on creating dropdownList with option groups
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.