Fixes #11865: Fixed setting selected for dropdown list using options

This commit is contained in:
Alexander Makarov
2016-07-03 01:43:32 +03:00
parent 2c021f5ab8
commit 16e4cab8f1
3 changed files with 16 additions and 1 deletions

View File

@ -47,6 +47,7 @@ Yii Framework 2 Change Log
- Bug #10681: Fixed active form `beforeValidate` wasn't triggered in some cases (lynicidn)
- Enh #11857: `yii\filters\AccessRule::$verbs` could not be configured with any case of request method names (DrDeath72, samdark)
- Bug #11863: Fixed usage of `mb_substr` with PHP < 5.4.8 where length of NULL was treated the same as 0 (samdark)
- Bug #11865: Fixed setting `selected` for dropdown list using options (samdark)
2.0.8 April 28, 2016
--------------------

View File

@ -1702,9 +1702,11 @@ class BaseHtml
} else {
$attrs = isset($options[$key]) ? $options[$key] : [];
$attrs['value'] = (string) $key;
$attrs['selected'] = $selection !== null &&
if (!array_key_exists('selected', $attrs)) {
$attrs['selected'] = $selection !== null &&
(!ArrayHelper::isTraversable($selection) && !strcmp($key, $selection)
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn($key, $selection));
}
$text = $encode ? static::encode($value) : $value;
if ($encodeSpaces) {
$text = str_replace(' ', '&nbsp;', $text);

View File

@ -280,6 +280,18 @@ EOD;
</select>
EOD;
$this->assertEqualsWithoutLE($expected, Html::dropDownList('test', 'value2', $this->getDataItems()));
$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2" selected>text2</option>
</select>
EOD;
$this->assertEqualsWithoutLE($expected, Html::dropDownList('test', null, $this->getDataItems(), [
'options' => [
'value2' => ['selected' => true]
],
]));
}
public function testListBox()