diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 52c7f52510..feeab8d4e9 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 -------------------- diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index c9975c4f62..914a0fb247 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -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(' ', ' ', $text); diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index 33032956e5..edf6a53b85 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -280,6 +280,18 @@ EOD; EOD; $this->assertEqualsWithoutLE($expected, Html::dropDownList('test', 'value2', $this->getDataItems())); + + $expected = << + + + +EOD; + $this->assertEqualsWithoutLE($expected, Html::dropDownList('test', null, $this->getDataItems(), [ + 'options' => [ + 'value2' => ['selected' => true] + ], + ])); } public function testListBox()