How to add prompt or placeholder to a drop down-list in Yii2?

How to add prompt or placeholder to a drop down-list in Yii2?
a
admin 03 May, 2020 - فى Yii2
640

Creating a drop-down list input in Yii2 framework is simple like the following example

<?= $form->field($model, 'language_id')->dropDownList([1 => 'item 1', 2 => 'item 2']) ?>
 
And the first option will be selected automatically, but if you want to force the user to choose one of these options, you will need to inject a placeholder (prompt) to this item. By adding the prompt feature a new empty option will be added to the front of this drop-down input. just customize it like the following.
  
<?= $form->field($model, 'language_id')->dropDownList([1 => 'item 1', 2 => 'item 2'], ['prompt' => 'Choose your option']) ?>

 

If your drop-down has many options and you want to enable the search in the list. You can use the great plugin of JQuery Select2 through the extension yii2-widget-select2 

 

 

 

دورات مجانية معتمدة