Search code examples
phpyii2optgroup

Yii2 dropdown optgroup label


i've created a drop down using the active form with the following code, but somehow the label for the optgroup is displayed by default "0", how can i make it to be hidden?

<?= $form->field($model, 'idcmp_usr')
    ->dropDownList([Company::find()
        ->select(['name_cmp','id_cmp'])
        ->indexBy('id_cmp')
        ->column()]); ?>

the displaying html :

<label class="control-label" for="user-idcmp_usr">Company</label>
<select id="user-idcmp_usr" class="form-control" name="User[idcmp_usr]" aria-required="true">
<optgroup label="0">
<option value="1">###</option>
<option value="2" selected>###</option>
<option value="13">###</option>
<option value="14">###</option>
</optgroup>
</select>

i have tried several things in css or with dropdown()->label(false) but it just won't dissapear


Solution

  • Remove extra []

    <?= $form->field($model, 'idcmp_usr')->dropDownList(Company::find()
        ->select(['name_cmp','id_cmp'])
        ->indexBy('id_cmp')
        ->column()
    ); ?>