Twig - Symfony enhanced radio not showing correctly

I have a form branch template with helpers and form blocks that I want to use to automatically format my form as Zurb Foundations expected.

It seems to work well, but I ran into a problem with advanced choices (switches), as you can see below.

enter image description here

Here is the generated markup:

<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>

For some reason, the shortcut for the "Male" option wraps the "Woman" option, and when corrected in a control element, this is normal.

Here is my branch template where I redefine the blocks:

{#
############# 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 -%}

Updated information, form type option:

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

Can anyone suggest a better layout for matching Foundation 5 Forms .

Best regards Nathan

+4
1

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

label. Male Female.

</label> .

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

Source: https://habr.com/ru/post/1598198/


All Articles