dropDownList without "strict" has new behavior in 2.0.46
Correction for issue https://github.com/yiisoft/yii2/issues/19324 causes new issue for dropDownList()
We have some dropDownList in search forms with both "empty string value" and "zero value", like this
<form action="..." method="get">
<select>
<option value="">- select -</option>
<option value="0">House handed over</option>
<option value="2022">2022 Year</option>
...
</select>
</form>
Strict parameter is not set for dropDownList().
First render of form we get no one option with "selected" attribute.
In Yii 2.0.46 after form submit and page reload without choosing any option in this <select> we get "zero value" option selected <option value="0" selected>
Before Yii 2.0.46 we get no one option selected, without strict => true In Yii 2.0.46 we have to set strict => true to reproduce previous behavior.
Is this a bug or a feature? Now we need to fix a lot of fields in many forms to fix the problem.
| Yii version | 2.0.46 | PHP version | 7.4 | Operating system | Ubuntu
I'm having the same issue. This completely messed up my gridview filters.
<select>
<option value=""></option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
Selecting the "blank" option automatically changes to "0".
@bizley would you please take a look at this isssue?
This is the same as in https://github.com/yiisoft/yii2/issues/19508 isn't it?
Maybe. @martynenko-dmitry please try master branch.
@samdark Nothing changed. Problem locates in renderSelectOptions() of helpers/BaseHtml.php
Previous condition was
$attrs['selected'] = $selection !== null && (!ArrayHelper::isTraversable($selection) && !strcmp($key, $selection) || ArrayHelper::isTraversable($selection) ...
Now condition is
$attrs['selected'] = $selection !== null && (!ArrayHelper::isTraversable($selection) && ($strict ? !strcmp($key, $selection) : $selection == $key) || ArrayHelper::isTraversable($selection) ...
Before submit $selection == null, after submit $selection == "".
$key for <option value="0"> has numeric zero value.
Without $strict => true, the new condition $selection == $key is used, that is, 0 == "" we get the result "true" for PHP 7 and "false" for PHP 8.
This is the same as in #19508 isn't it?
@bizley I think it the same issue
behavior changed in #19324
we have the same issue.
if i am right it will be fixed in https://github.com/yiisoft/yii2/pull/19524 @WinterSilence
@papppeter nope, it's cant be fixed, only rollback. #19524 fixed issue #19522
@papppeter @canreo @martynenko-dmitry vote in #19580
could you maybe release this as a fix? 2.0.46.1? we could not switch to 2.0.46 because of this and https://github.com/yiisoft/yii2/issues/19581
Would it be fixed if we'll change strict default value?
Would it be fixed if we'll change
strictdefault value?
Yes, it will help for PHP 7.x in our case - for numeric and string values in selects.
But we don't use boolean values for selectors (https://github.com/yiisoft/yii2/issues/19324). I can't say anything about how "strict" will affect in their case.