【问题标题】:Twig - Symfony radio expanded not rendered correctlyTwig - Symfony 收音机扩展未正确渲染
【发布时间】:2015-07-16 10:57:08
【问题描述】:

我有一个带有助手和表单块的表单树枝模板,我想用它来按照 Zurb Foundations 期望的方式自动设置表单样式。

它似乎大部分都运行良好,但我遇到了选择扩展(单选按钮)的问题,如下所示。

这里是生成标记:

<div class="large-12 columns">
   <input type="radio" id="user_gender_0" name="user[gender]" required="required" class="" value="male">                <label class="is-required">Male<input type="radio" id="user_gender_1" name="user[gender]" required="required" class="" value="female">                <label class="is-required">Female</label></label>
</div>

由于某种原因,“男性”选项的标签包裹了“女性”选项,并且在检查元素中更正后就可以了。

这是我覆盖块的树枝模板:

{#
############# Radio #############
#}

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


{#
############# Labels #############
#}

{%- block form_label -%}
    {% if label is not sameas(false) -%}
        {% if required -%}
            {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' is-required')|trim}) %}
        {%- endif %}
        {% if errors|length > 0 -%}
            {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' error')|trim}) %}
        {% endif %}
        {% if label is empty -%}
            {%- if label_format is not empty -%}
                {% set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) %}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}
        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}
    {%- endif -%}
{%- endblock form_label -%}

{%- block button_label -%}{%- endblock -%}

更新信息,表单类型选项:

->add('gender', 'choice', [
                'constraints' => new NotBlank(),
                'choices' => Profile::getGenderTypes(),
                'expanded' => true,
                'multiple' => false,
                'mapped' => false,
                'attr' => [
                    'data-user-form' => 'gender'
                ]
            ])

谁能推荐一个更好的布局来匹配Foundation 5 Forms

最诚挚的问候 内森

【问题讨论】:

    标签: php symfony twig zurb-foundation-5


    【解决方案1】:

    在这一行

    <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}
    

    您没有关闭label 标记。这就是Male 标签包裹Female 选项的原因。

    解决方案

    在末尾添加&lt;/label&gt;

    <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
    

    【讨论】:

    • 好地方,我应该给那个打卡的...谢谢你的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多