4

I have a form twig template with helpers and form blocks which I want to use to automatically style my form in the way Zurb Foundations expects.

It seems to be working well mostly but I have run into an issue with choice expanded (radio buttons) as you can see below.

enter image description here

Here is the generate 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 label for the "Male" option is wrapping the "Female" option and when corrected in inspect element it's fine.

Here is my twig template where I override 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 info, the 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 to match Foundation 5 Forms.

Kindest Regards Nathan

Kal
  • 2,022
  • 5
  • 31
  • 71

1 Answers1

3

In this line

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

You didn't close the label tag. That's why the Male label is wrapping the Female option.

Solution

Add </label> at the end.

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