yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

dropDownList without "strict" has new behavior in 2.0.46

Open martynenko-dmitry opened this issue 3 years ago • 10 comments

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

martynenko-dmitry avatar Sep 02 '22 09:09 martynenko-dmitry

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".

canreo avatar Sep 02 '22 15:09 canreo

@bizley would you please take a look at this isssue?

samdark avatar Sep 04 '22 19:09 samdark

This is the same as in https://github.com/yiisoft/yii2/issues/19508 isn't it?

bizley avatar Sep 04 '22 20:09 bizley

Maybe. @martynenko-dmitry please try master branch.

samdark avatar Sep 05 '22 21:09 samdark

@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.

martynenko-dmitry avatar Sep 06 '22 11:09 martynenko-dmitry

This is the same as in #19508 isn't it?

@bizley I think it the same issue

martynenko-dmitry avatar Sep 06 '22 11:09 martynenko-dmitry

behavior changed in #19324

WinterSilence avatar Sep 10 '22 02:09 WinterSilence

we have the same issue.

if i am right it will be fixed in https://github.com/yiisoft/yii2/pull/19524 @WinterSilence

papppeter avatar Sep 15 '22 08:09 papppeter

@papppeter nope, it's cant be fixed, only rollback. #19524 fixed issue #19522

WinterSilence avatar Sep 15 '22 08:09 WinterSilence

@papppeter @canreo @martynenko-dmitry vote in #19580

WinterSilence avatar Sep 15 '22 12:09 WinterSilence

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

papppeter avatar Nov 11 '22 15:11 papppeter

Would it be fixed if we'll change strict default value?

samdark avatar Nov 15 '22 07:11 samdark

Would it be fixed if we'll change strict default 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.

martynenko-dmitry avatar Nov 15 '22 14:11 martynenko-dmitry