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.

Published by Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.

Join the Conversation

3 Comments

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

Leave a comment

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