New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dropDownList pre Selection not rendering 'selected' #2728

Closed
ghost opened this Issue Mar 13, 2014 · 16 comments

Comments

Projects
None yet
8 participants
@ghost

ghost commented Mar 13, 2014

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>
@tsvetann

This comment has been minimized.

Show comment
Hide comment
@tsvetann

tsvetann Mar 13, 2014

Contributor

Pretty interesting situation. I had a quick look and it seems that inBaseHtml::renderSelectOptions() on a new record $selection is always null and your selected option gets false. The following change seems to work for me:

$attrs['selected'] = $selection !== null &&
                    (!is_array($selection) && !strcmp($key, $selection)
                        || is_array($selection) && in_array($key, $selection))
                        || $selection === null && isset($attrs['selected']);
Contributor

tsvetann commented Mar 13, 2014

Pretty interesting situation. I had a quick look and it seems that inBaseHtml::renderSelectOptions() on a new record $selection is always null and your selected option gets false. The following change seems to work for me:

$attrs['selected'] = $selection !== null &&
                    (!is_array($selection) && !strcmp($key, $selection)
                        || is_array($selection) && in_array($key, $selection))
                        || $selection === null && isset($attrs['selected']);
@drenty

This comment has been minimized.

Show comment
Hide comment
@drenty

drenty Mar 18, 2014

Contributor

I confirm this change makes sense. It is necessary as we need to have a way to define a default value on a new record.

However it's not very practical, I wonder if we could have a $defaultSelection parameter in ActiveField::dropDownList. Maybe it's too much change though.

Contributor

drenty commented Mar 18, 2014

I confirm this change makes sense. It is necessary as we need to have a way to define a default value on a new record.

However it's not very practical, I wonder if we could have a $defaultSelection parameter in ActiveField::dropDownList. Maybe it's too much change though.

@tsvetann

This comment has been minimized.

Show comment
Hide comment
@tsvetann

tsvetann Mar 18, 2014

Contributor

Generally, we have two main scenarios here. New record and updating an existing record. With the change I highlighted above the custom selected option will be set only on new records. On existing records the selected option will be determined by the record persistent value and the user provided one will be ignored. Not sure if this is ideal it's just an example. Can you expand on how you anticipate this $defaultSelection to behave?

Contributor

tsvetann commented Mar 18, 2014

Generally, we have two main scenarios here. New record and updating an existing record. With the change I highlighted above the custom selected option will be set only on new records. On existing records the selected option will be determined by the record persistent value and the user provided one will be ignored. Not sure if this is ideal it's just an example. Can you expand on how you anticipate this $defaultSelection to behave?

@drenty

This comment has been minimized.

Show comment
Hide comment
@drenty

drenty Mar 18, 2014

Contributor

Exactly the way you described it, just an additional parameter to dropDownList's signature as a shortcut for setting

['options' =>
    [
        $defaultSelection => ['selected' => true]
    ]
]

It is just an idea, I'm not sure of its validity though. Your change might be enough.

Contributor

drenty commented Mar 18, 2014

Exactly the way you described it, just an additional parameter to dropDownList's signature as a shortcut for setting

['options' =>
    [
        $defaultSelection => ['selected' => true]
    ]
]

It is just an idea, I'm not sure of its validity though. Your change might be enough.

@RusAlex

This comment has been minimized.

Show comment
Hide comment
@RusAlex

RusAlex Mar 19, 2014

Contributor

Why you expect field will set selected for a specific value if $model->$attribute is empty ?

Contributor

RusAlex commented Mar 19, 2014

Why you expect field will set selected for a specific value if $model->$attribute is empty ?

@drenty

This comment has been minimized.

Show comment
Hide comment
@drenty

drenty Mar 19, 2014

Contributor

The idea is to be able to set a default value when the value is not set (in a new record for instance).

Example: I have a dropdown with languages, I want to set the default option to English.

Contributor

drenty commented Mar 19, 2014

The idea is to be able to set a default value when the value is not set (in a new record for instance).

Example: I have a dropdown with languages, I want to set the default option to English.

@RusAlex

This comment has been minimized.

Show comment
Hide comment
@RusAlex

RusAlex Mar 19, 2014

Contributor

Why not just to set new record pre-defined values in init() method ?

Contributor

RusAlex commented Mar 19, 2014

Why not just to set new record pre-defined values in init() method ?

@drenty

This comment has been minimized.

Show comment
Hide comment
@drenty

drenty Mar 19, 2014

Contributor

Indeed...

Contributor

drenty commented Mar 19, 2014

Indeed...

@RusAlex

This comment has been minimized.

Show comment
Hide comment
@RusAlex

RusAlex Mar 19, 2014

Contributor

This is how i solve this problem.

Contributor

RusAlex commented Mar 19, 2014

This is how i solve this problem.

@samdark samdark removed the type:bug label Mar 19, 2014

@samdark

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Mar 19, 2014

Member

So do you need any adjustments?

Member

samdark commented Mar 19, 2014

So do you need any adjustments?

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Mar 20, 2014

@RusAlex
i had this issue when updating records, in that case $model->attribute was given. When i was trying to set the selected option via an html option it just wouldn't render if there wasn't a whitespace behind the selected attribute. But as i found a different way to select it, probably the right way, that's solved for me then 👍

@samdark: for me, its fine now 👍

ghost commented Mar 20, 2014

@RusAlex
i had this issue when updating records, in that case $model->attribute was given. When i was trying to set the selected option via an html option it just wouldn't render if there wasn't a whitespace behind the selected attribute. But as i found a different way to select it, probably the right way, that's solved for me then 👍

@samdark: for me, its fine now 👍

@samdark samdark closed this Mar 20, 2014

@drenty

This comment has been minimized.

Show comment
Hide comment
@drenty

drenty Mar 22, 2014

Contributor

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.

Contributor

drenty commented Mar 22, 2014

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.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Mar 23, 2014

Member

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

the field value should get overridden when data is loaded from db.
See https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L1034

Member

cebe commented Mar 23, 2014

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

the field value should get overridden when data is loaded from db.
See https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L1034

@matjaz-elumina

This comment has been minimized.

Show comment
Hide comment
@matjaz-elumina

matjaz-elumina Jan 25, 2015

I think there should be another reserved option (defaultValue or something) in the options attribute where you would define which option is selected by default (in case $model->isNewRecord is true).

All other solutions mentioned here aren't the best in my opinion. A lot of times this pre-defined option is a matter of just certain view (form) and you don't want to define it globally (in init()).

matjaz-elumina commented Jan 25, 2015

I think there should be another reserved option (defaultValue or something) in the options attribute where you would define which option is selected by default (in case $model->isNewRecord is true).

All other solutions mentioned here aren't the best in my opinion. A lot of times this pre-defined option is a matter of just certain view (form) and you don't want to define it globally (in init()).

@RadikChernyshov

This comment has been minimized.

Show comment
Hide comment
@RadikChernyshov

RadikChernyshov Nov 2, 2015

I have the same problem.

<?php echo $form->field($malfunctionModel, 'category_id')->dropDownList(
   ArrayHelper::map($categoryModel, 'id', 'title'), [
      'options' => [
         3 => ['selected' => 'selected']
     ]
]); ?>

This doesn't work.
But ['selected ' => 'selected'] - works. Tab after selected )))

RadikChernyshov commented Nov 2, 2015

I have the same problem.

<?php echo $form->field($malfunctionModel, 'category_id')->dropDownList(
   ArrayHelper::map($categoryModel, 'id', 'title'), [
      'options' => [
         3 => ['selected' => 'selected']
     ]
]); ?>

This doesn't work.
But ['selected ' => 'selected'] - works. Tab after selected )))

@Morr123

This comment has been minimized.

Show comment
Hide comment
@Morr123

Morr123 commented Jun 13, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment