Skip to content

Commit aa0d36a

Browse files
author
Bizley
authored
Fix #19534: Fix yii\helpers\BaseHtml::renderSelectOptions() to properly render boolean selection
1 parent 64f2451 commit aa0d36a

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Yii Framework 2 Change Log
2121
- Bug #19520: Fix for TIMESTAMP & ROWVERSION columns in MSSQL insert query (darkdef)
2222
- Bug #19581: Fix regression in `CompositeAuth` introduced in #19418 (SamMousa, WinterSilence, samdark)
2323
- Chg #17811: Do not reset `retryHandler` when `yii\db\Command::reset()` called (erickskrauch)
24+
- Bug #19534: Fix `yii\helpers\BaseHtml::renderSelectOptions()` to properly render boolean selection (bizley)
2425

2526

2627
2.0.46 August 18, 2022

helpers/BaseHtml.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,8 @@ protected static function booleanInput($type, $name, $checked = false, $options
792792
/**
793793
* Generates a drop-down list.
794794
* @param string $name the input name
795-
* @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s).
795+
* @param string|bool|array|null $selection the selected value(s). String/boolean for single or array for multiple
796+
* selection(s).
796797
* @param array $items the option data items. The array keys are option values, and the array values
797798
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
798799
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
@@ -849,7 +850,8 @@ public static function dropDownList($name, $selection = null, $items = [], $opti
849850
/**
850851
* Generates a list box.
851852
* @param string $name the input name
852-
* @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s).
853+
* @param string|bool|array|null $selection the selected value(s). String for single or array for multiple
854+
* selection(s).
853855
* @param array $items the option data items. The array keys are option values, and the array values
854856
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
855857
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
@@ -1854,7 +1856,8 @@ protected static function activeListInput($type, $model, $attribute, $items, $op
18541856

18551857
/**
18561858
* Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]].
1857-
* @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s).
1859+
* @param string|array|bool|null $selection the selected value(s). String/boolean for single or array for multiple
1860+
* selection(s).
18581861
* @param array $items the option data items. The array keys are option values, and the array values
18591862
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
18601863
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
@@ -1872,7 +1875,17 @@ protected static function activeListInput($type, $model, $attribute, $items, $op
18721875
public static function renderSelectOptions($selection, $items, &$tagOptions = [])
18731876
{
18741877
if (ArrayHelper::isTraversable($selection)) {
1875-
$selection = array_map('strval', ArrayHelper::toArray($selection));
1878+
$normalizedSelection = [];
1879+
foreach (ArrayHelper::toArray($selection) as $selectionItem) {
1880+
if (is_bool($selectionItem)) {
1881+
$normalizedSelection[] = $selectionItem ? '1' : '0';
1882+
} else {
1883+
$normalizedSelection[] = (string)$selectionItem;
1884+
}
1885+
}
1886+
$selection = $normalizedSelection;
1887+
} elseif (is_bool($selection)) {
1888+
$selection = $selection ? '1' : '0';
18761889
}
18771890

18781891
$lines = [];
@@ -1913,9 +1926,20 @@ public static function renderSelectOptions($selection, $items, &$tagOptions = []
19131926
$attrs = isset($options[$key]) ? $options[$key] : [];
19141927
$attrs['value'] = (string) $key;
19151928
if (!array_key_exists('selected', $attrs)) {
1916-
$attrs['selected'] = $selection !== null &&
1917-
(!ArrayHelper::isTraversable($selection) && ($strict ? !strcmp($key, $selection) : $selection == $key)
1918-
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$key, $selection, $strict));
1929+
$selected = false;
1930+
if ($selection !== null) {
1931+
if (ArrayHelper::isTraversable($selection)) {
1932+
$selected = ArrayHelper::isIn((string)$key, $selection, $strict);
1933+
} elseif ($key === '' || $selection === '') {
1934+
$selected = $selection === $key;
1935+
} elseif ($strict) {
1936+
$selected = !strcmp((string)$key, (string)$selection);
1937+
} else {
1938+
$selected = $selection == $key;
1939+
}
1940+
}
1941+
1942+
$attrs['selected'] = $selected;
19191943
}
19201944
$text = $encode ? static::encode($value) : $value;
19211945
if ($encodeSpaces) {

0 commit comments

Comments
 (0)