Yii2: Wrong logic generate HTML dataset attribute

Created on 17 Feb 2020  路  6Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

Previously, the generation of dataset attributes when passing true or false, converted to 1 or 0. Now the logic has changed. Maybe worth a review #17625?

What is the expected result?

echo Html::tag('div', 'TEST', ['data' => ['id' => 'main_div', 'test' => true]]);

generate HTML code

<div data-id="main_div" data-test="1">TEST</div>

What do you get instead?

TRUE

echo Html::tag('div', 'TEST', ['data' => ['id' => 'main_div', 'test' => true]]);

generate HTML code

<div data-id="main_div" data-test="">TEST</div>

FALSE

echo Html::tag('div', 'TEST', ['data' => ['id' => 'main_div', 'test' => false]]);

generate HTML code

<div data-id="main_div">TEST</div>

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.32
| PHP version | 7.3.11
| Operating system | Unix

help wanted ready for adoption docs

Most helpful comment

@s1lver even if it does, it was released in 2.0.29. It's three versions ago so already adopted. Reverting it would be backwards compatibility break as well. What we can do is adding a note to UPGRADE.

All 6 comments

I fix it

elseif (is_bool($v)) {
    if ($v) {
        $html .= " $name-$n=" .$v;
    }
 } 

Do you need the 1/0 on the attribute?
You could pass an int/string to get a value.

The new logic provides for the presence of the attribute itself. It breaks backward compatibility

I'd say the update brings it into line with the html spec, https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attribute

@s1lver even if it does, it was released in 2.0.29. It's three versions ago so already adopted. Reverting it would be backwards compatibility break as well. What we can do is adding a note to UPGRADE.

Ok

Was this page helpful?
0 / 5 - 0 ratings