dongqing483174 2016-10-19 15:54
浏览 35
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dongliyan9190 2016-10-19 16:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?