Twig 中的自定义单选按钮 ID

Custom radio button id in Twig

呈现的 Twigs 单选按钮集表单的代码通常看起来像...

这个:

 {{ form_widget(formTitle.radioSet)  }}

为此...

<input type="radio" id="formTitle_radioSet_0" name="formTitle[radioSet]" value="fooValue">
<input type="radio" id="formTitle_radioSet_1" name="formTitle[radioSet]" value="booValue">

...还有几行像这样

我想让它更容易与 JS 一起工作。是否可以像这样呈现自定义单选按钮,其中的 ID 被引用到单选按钮的值和我自己的偏好??

<input type="radio" id="radioSet_fooValue" name="formTitle[radioSet]"  value="fooValue">
<input type="radio" id="radioSet_booValue" name="formTitle[radioSet]"  value="fooValue">

使用custom form rendering

您只需要使用该食谱条目

中描述的技术之一自定义 widget_radio

经过大约半小时探索 Carlos Granados 推荐的文章后得到了解决方案。目标是覆盖渲染无线电输入的树枝模板。

简而言之,粘贴在网页模板开头的以下代码将覆盖所有收音机的呈现方式,其描述方式有问题。

{% form_theme form _self %}
{%- block radio_widget -%}
<input type="radio" name="{{ full_name }}" id="radio_{{ value }}" value="{{ value }}"{% if checked %} checked="checked"{% endif %} />
{%- endblock radio_widget -%}