Skip to content

Commit

Permalink
Fix #19534: Fix yii\helpers\BaseHtml::renderSelectOptions() to prop…
Browse files Browse the repository at this point in the history
…erly render boolean selection
  • Loading branch information
Bizley committed Nov 17, 2022
1 parent 64f2451 commit aa0d36a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Yii Framework 2 Change Log
- Bug #19520: Fix for TIMESTAMP & ROWVERSION columns in MSSQL insert query (darkdef)
- Bug #19581: Fix regression in `CompositeAuth` introduced in #19418 (SamMousa, WinterSilence, samdark)
- Chg #17811: Do not reset `retryHandler` when `yii\db\Command::reset()` called (erickskrauch)
- Bug #19534: Fix `yii\helpers\BaseHtml::renderSelectOptions()` to properly render boolean selection (bizley)


2.0.46 August 18, 2022
Expand Down
38 changes: 31 additions & 7 deletions helpers/BaseHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ protected static function booleanInput($type, $name, $checked = false, $options
/**
* Generates a drop-down list.
* @param string $name the input name
* @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s).
* @param string|bool|array|null $selection the selected value(s). String/boolean for single or array for multiple
* selection(s).
* @param array $items the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
Expand Down Expand Up @@ -849,7 +850,8 @@ public static function dropDownList($name, $selection = null, $items = [], $opti
/**
* Generates a list box.
* @param string $name the input name
* @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s).
* @param string|bool|array|null $selection the selected value(s). String for single or array for multiple
* selection(s).
* @param array $items the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
Expand Down Expand Up @@ -1854,7 +1856,8 @@ protected static function activeListInput($type, $model, $attribute, $items, $op

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

$lines = [];
Expand Down Expand Up @@ -1913,9 +1926,20 @@ public static function renderSelectOptions($selection, $items, &$tagOptions = []
$attrs = isset($options[$key]) ? $options[$key] : [];
$attrs['value'] = (string) $key;
if (!array_key_exists('selected', $attrs)) {
$attrs['selected'] = $selection !== null &&
(!ArrayHelper::isTraversable($selection) && ($strict ? !strcmp($key, $selection) : $selection == $key)
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$key, $selection, $strict));
$selected = false;
if ($selection !== null) {
if (ArrayHelper::isTraversable($selection)) {
$selected = ArrayHelper::isIn((string)$key, $selection, $strict);
} elseif ($key === '' || $selection === '') {
$selected = $selection === $key;
} elseif ($strict) {
$selected = !strcmp((string)$key, (string)$selection);
} else {
$selected = $selection == $key;
}
}

$attrs['selected'] = $selected;
}
$text = $encode ? static::encode($value) : $value;
if ($encodeSpaces) {
Expand Down

0 comments on commit aa0d36a

Please sign in to comment.