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?

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 

 

 

 

درس القراء ايضا

© حقوق النشر محفوظة لمالكى القنوات | القوانين والخصوصية

اضف التطبيق الى شاشتك الرئيسية