Symfony形式:它是否省略了`false``attr`?

I want to know in JavaScript if a particular feature is active or not.

I have a PHP class that has some has* methods.

So, in Twig I do this:

{{ form_widget(form.plan.seo, {'attr': {'class': 'feature', 'data-already-active': store.premium.hasSeo}}) }}

I expect it sets data-already-active to 0 or to 1 depending on the feature is active or not.

But the generated HTML is this if the has* method returns true:

<input type="checkbox" id="form_plan_seo" name="form[plan][seo]" class="feature" data-already-active="data-already-active" value="1" checked="checked">

while simply omits the data-already-active attribute if the has* method returns false:

<input type="checkbox" id="form_plan_social" name="form[plan][social]" class="feature" value="1">

More, has you can see, the value of the attribute data-already-active is not 0 or 1 nor true or false but is the name of the attribute itself:

data-already-active="data-already-active"

Is this normal? Have I a better way of setting this information in the checkbox?

Yep, that seems to be the normal behavior for assigning boolean values for the twig attrs values. An easy workaround would be to modify the code a bit:

{{ form_widget(form.plan.seo, {'attr': {'class': 'feature', 'data-already-active': store.premium.hasSeo ? "1" : "0"}}) }}

or if you want to have this working with your current twig definition you can also move this boolean to string logic into the form template.