How to make a drop down list in yii2?

How to make a drop down list in yii2?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Dropdowns in Yii 2 with ActiveForm

In Yii 2, creating a dropdown list within an ActiveForm is straightforward once you understand that the framework handles the heavy lifting of binding data to HTML elements. The core mechanism relies on the list() helper method provided by the ActiveField class, which automatically generates a <select> tag populated with options from your model or a static array.

To begin, ensure you have a model with a field intended for selection. For example, consider a User model with a status attribute that accepts integer values: 0 for inactive and 1 for active. In the controller action, pass this model to the view along with a list of statuses. The view then renders an ActiveForm where you define the input using the standard syntax:

use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;

// Inside your view file
$form = ActiveForm::begin();

echo $form->field($model, 'status')->dropDownList(
    [
        0 => 'Inactive',
        1 => 'Active',
        2 => 'Pending', // Example additional option
    ],
    [
        'prompt' => 'Select Status',
        'class' => 'form-control',
    ]
);

ActiveForm::end();

This approach works seamlessly because dropDownList accepts a simple array where keys represent the value submitted to the server and values represent the text displayed in the interface. The $model->status attribute is automatically bound to the generated HTML element, ensuring that when the form submits, the selected integer is passed correctly for validation and storage.

If you prefer to generate options dynamically based on database content rather than a static array, you can pass a result set from a query. Suppose your User model has a related Role model. You can define the dropdown options by fetching distinct roles:

$roles = ArrayHelper::map($this->roleManager->getRoles(), 'id', 'name');

echo $form->field($model, 'role_id')->dropDownList(
    $roles + ['' => '-- Select Role --'],
    [
        'prompt' => '-- Select Role --',
    ]
);

The ArrayHelper::map function is essential here. It converts an array of objects (typically from Eloquent queries or ActiveRecord models) into the associative array format required by dropDownList. The first argument provides the key for the dropdown option, while the second provides the label. Adding a blank option with a prompt is a common pattern to indicate that no selection has been made yet, which often simplifies default value handling in your model's validation rules.

One critical detail involves handling multiple selections. While dropDownList creates a single-selection <select> by default, you can enable multiple choices by setting the multiple attribute to true. However, this changes the HTML structure significantly, as the form field will submit an array of values rather than a single scalar. Your model's validation rules and type casting logic must be updated accordingly to handle this difference.

Another consideration is accessibility and user experience. Always ensure that every <select> element has a corresponding label, which ActiveForm::field() handles automatically if you omit the label parameter in the field definition or explicitly configure it. Furthermore, if you are using custom classes for styling, applying them via the second argument of dropDownList ensures consistency with your site's design system without needing additional CSS files.

For more complex scenarios involving dynamic option generation based on other form fields (such as showing different states depending on a selected region), Yii 2 supports JavaScript event listeners to update the dropdown dynamically. This often involves using jQuery or vanilla JS to listen for changes on parent inputs and re-render the options list, maintaining the bound model state throughout the process.

When working with large datasets, consider performance implications. Rendering thousands of options in a single dropdown can degrade user experience and browser performance. In such cases, implementing virtual scrolling or pagination logic might be necessary, though these advanced patterns usually require custom JavaScript implementation outside the standard ActiveForm helpers.

Our network