yii2: dropDownList pre Selection not rendering 'selected'

The issue

It doesn’t select the item unless i have a trailing or leading whitespace before or after the attribute word selected.

In my Code bellow, this is how i setup the dropdownlist and set the ‘selected’ attribute to true or to ‘selected’.

This Issue only appears just for ‘selected’, if i put ‘disabled’, ‘class’, ‘myfunnyattribute’ i renders fine or gets at least displayed in the source code.

My Config

Apache 2.4.6 PHP 5.5.3 Firefox 27.0.1

Inside the View:
$form->field($model, $key, 
                        ['options' => 
                            [
                                'class' => 'col-xs-3'
                            ]
                        ])
                    ->dropDownList(
                        $model->getTeamName(),
                        ['options' =>
                            [
                                $user->$key => ['selected ' => true]
                            ]
                        ]
                    );
Function in Model:
public function getTeamname()
{
    $team = Team::find()->orderBy('teamName')->all();
    return ArrayHelper::map($team, 'teamID', 'teamName');
}
Renderoutput with [‘selected’ => ‘selected’]
<option value="9">
    IT Administration
</option>
Renderoutput with ['selected ’ => ‘selected’]
<option selected="selected" value="9">
    IT Administration
</option>

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 16 (12 by maintainers)

Most upvoted comments

The best way IMO is to do this directly in the form view:

if($model->isNewRecord) {
    $model->foo = $myDefaultValue;
}

If you set it in init() it will be set whenever a query returns active records which is not great.