How to make a drop down list in yii2?

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.

0 0 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Nagaraju Kakaraparthi
Nagaraju Kakaraparthi
4 years ago

$customer_persons_data = (new yiidbQuery())
->select([‘customer_person_id as id’, “CONCAT(name, ‘ (‘,email,’)’) as name”])
->where([‘customer_person_id’ => $customer_persons_ids])

3 years ago


3 years ago

hello Protection Status
Would love your thoughts, please comment.x