Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 dropdown empty option

How to implement the following Yii code to Yii2:

<?php 
   echo $form->dropDownList($model, 
                           'project', 
                           $model->getProjectOptions(), 
                           array('empty' => 'Empty string')
   ); 
?>
like image 690
Nahid Hossain Avatar asked Jun 12 '13 07:06

Nahid Hossain


Video Answer


2 Answers

Why not

<?
    dropDownList($model, 
        'project', 
        $model->getProjectOptions(), 
        array('prompt'=>'Empty string')
    ); ?>
  • prompt: string, a prompt text to be displayed as the first option;

Here is old CHtml https://github.com/yiisoft/yii2/blob/master/framework/yii/helpers/base/Html.php

Can find there if you need something more.

like image 58
ineersa Avatar answered Oct 04 '22 07:10

ineersa


Use the following code to get the dropdownlist in yii2 friend.

<?php 
    //use app\models\Country;
    $countries=Country::find()->all();

    //use yii\helpers\ArrayHelper;
    $listData=ArrayHelper::map($countries,'code','name');

    echo $form->field($model, 'name')->dropDownList(
                                    $listData, 
                                    ['prompt'=>'Select...']);
    ?>
like image 41
sarin surendran Avatar answered Oct 04 '22 05:10

sarin surendran