@@ -792,7 +792,8 @@ protected static function booleanInput($type, $name, $checked = false, $options
792
792
/**
793
793
* Generates a drop-down list.
794
794
* @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).
796
797
* @param array $items the option data items. The array keys are option values, and the array values
797
798
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
798
799
* 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
849
850
/**
850
851
* Generates a list box.
851
852
* @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).
853
855
* @param array $items the option data items. The array keys are option values, and the array values
854
856
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
855
857
* 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
1854
1856
1855
1857
/**
1856
1858
* 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).
1858
1861
* @param array $items the option data items. The array keys are option values, and the array values
1859
1862
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
1860
1863
* 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
1872
1875
public static function renderSelectOptions ($ selection , $ items , &$ tagOptions = [])
1873
1876
{
1874
1877
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 ' ;
1876
1889
}
1877
1890
1878
1891
$ lines = [];
@@ -1913,9 +1926,20 @@ public static function renderSelectOptions($selection, $items, &$tagOptions = []
1913
1926
$ attrs = isset ($ options [$ key ]) ? $ options [$ key ] : [];
1914
1927
$ attrs ['value ' ] = (string ) $ key ;
1915
1928
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 ;
1919
1943
}
1920
1944
$ text = $ encode ? static ::encode ($ value ) : $ value ;
1921
1945
if ($ encodeSpaces ) {
0 commit comments