Symfony2:防止单选小部件渲染标签

发布于 2024-12-18 18:35:13 字数 1620 浏览 0 评论 0原文

我正在尝试使用 symfony2 中的 Twig 自定义表单布局。我的目标是渲染一个看起来像这样的无线电输入...

<label class=" required">Label name</label>
<span class="form-radio">
    <input type="radio" name="album_has_subalbums_1" /> Yes
</span>
<span class="form-radio">
    <input type="radio" name="album_has_subalbums_0" /> No
</span>

我已经覆盖了自定义表单主题中的 radio_widget 块,如下所示

{% block radio_widget %}
{% spaceless %}
    <span class='form-radio'>
        <input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    </span>
{% endspaceless %}
{% endblock radio_widget %}

但是,这会呈现以下标记:

<label class=" required">Label name</label>

<span class="form-radio">
    <input type="radio" id="album_has_subalbums_1" name="album[has_subalbums]" required="required" value="1">
</span>
<label for="album_has_subalbums_1" class=" required">Yes</label>

<span class="form-radio">
    <input type="radio" id="album_has_subalbums_0" name="album[has_subalbums]" required="required" value="0">
</span>
<label for="album_has_subalbums_0" class=" required">No</label>

基本上,对于每个无线电输入它创建一个标签来识别它的值是是还是否。我正在使用预先存在的设计,因此我无法轻松调整 html 标记。

如何防止无线电输入生成选择文本作为标签?我知道它在内部调用 field_label 块,但是正如你所看到的,我的 radio_widget 没有引用它,所以我对如何防止它有点迷失这种行为。

编辑:

要明确的是,我想要与第一个示例相同的结构......我省略了名称和值属性等,但显然它只是为了演示目的。

I'm attempting to customise the form layouts using Twig in symfony2. I am aiming to render a radio input that looks something like this...

<label class=" required">Label name</label>
<span class="form-radio">
    <input type="radio" name="album_has_subalbums_1" /> Yes
</span>
<span class="form-radio">
    <input type="radio" name="album_has_subalbums_0" /> No
</span>

I have overridden the radio_widget block in my custom form theme as follows

{% block radio_widget %}
{% spaceless %}
    <span class='form-radio'>
        <input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    </span>
{% endspaceless %}
{% endblock radio_widget %}

However, this renders the following markup:

<label class=" required">Label name</label>

<span class="form-radio">
    <input type="radio" id="album_has_subalbums_1" name="album[has_subalbums]" required="required" value="1">
</span>
<label for="album_has_subalbums_1" class=" required">Yes</label>

<span class="form-radio">
    <input type="radio" id="album_has_subalbums_0" name="album[has_subalbums]" required="required" value="0">
</span>
<label for="album_has_subalbums_0" class=" required">No</label>

Basically, for each radio input element its creating a label to identify whether the value for it is Yes or No. I'm working with a pre-existing design so I can't easily tweak the html markup.

How can I prevent the radio inputs from generating the selection texts as labels? I know it calls the field_label block internally, but as you can see my radio_widget doesn't make reference to it, so I'm a little lost as to how to prevent this behaviour.

EDIT:

To be clear, I want the same kind of structure as my first example... I have left out the name and value attributes etc, but obviously its just for demonstration purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2024-12-25 18:35:13

您需要覆盖的不是 radio_widget 块,而是 choice_widget 块:

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
        <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {{ form_widget(child) }}
            {{ child.get('label') | trans }} {# <- this is what you need #}
    {# leave the rest untouched #}

并且不要忘记清除缓存以使此更改生效。

What you need to override is not the radio_widget block, but the choice_widget one:

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
        <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {{ form_widget(child) }}
            {{ child.get('label') | trans }} {# <- this is what you need #}
    {# leave the rest untouched #}

And don't forget to clear the cache for this change to take effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文