Is there a way to wrap a shortcut around input in symfony2?

Question about the symfony2 form component and its template:

I have a set of style labels (about 10 in one line). I usually use the tag <label>as follows: <label><input/> some text</label>but I cannot find a way to change it in the form template (form_div_layout.html.twig). I can’t even find a way to wrap any tag around the input widget and its label, and I always get the markup as follows: <input/> <some_tag><label>some text</label></some_tag>or <some_tag><input/></some_tag> <label>some text</label>, which is not very useful, to say the least ...

Quite a bit, but could not find the answer.

+5
source share
4 answers

I think this is what you are looking for: http://symfony.com/doc/current/book/templating.html#overriding-bundle-templates

, / .

form_div_layout.html.twig, /Resources/TwigBundle/views/Form/form _div_layout.html.twig, , symfony , .

EDIT: , , {% block checkbox_widget %}, , , vig

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{label|trans }} 
  <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} /> 
</label> 

"generic_label", .

+4

Whistlegreg , . {% block generic_label %}. . , , .

, {% block choice_widget %} {{ form_label(child) }} {% if expanded %}, .

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
        <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {{ form_widget(child) }}
        {#  {{ form_label(child) }} <--------------------- remove this line #}  
        {% endfor %}
        </div>
    {% else %}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {% if empty_value is not none %}
            <option value="">{{ empty_value|trans }}</option>
        {% endif %}
        {% if preferred_choices|length > 0 %}
            {% set options = preferred_choices %}
            {{ block('widget_choice_options') }}
            {% if choices|length > 0 and separator is not none %}
                <option disabled="disabled">{{ separator }}</option>
            {% endif %}
        {% endif %}
        {% set options = choices %}
        {{ block('widget_choice_options') }}
    </select>
    {% endif %}
{% endspaceless %}
{% endblock choice_widget %}

{% block checkbox_widget %}.

{% block checkbox_widget %}
{% spaceless %}
    <label  for="{{ id }}"><input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />{{ label|trans }}</label>
{% endspaceless %}
{% endblock checkbox_widget %}

{% block radio_widget %}, .

{% block radio_widget %}
{% spaceless %}
    <label  for="{{ id }}"><input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />{{ label|trans }}</label>
{% endspaceless %}
{% endblock radio_widget %}
+3

, . {% block checkbox_row %}. : http://forum.symfony-project.org/viewtopic.php?f=23&t=57986#p153546

, , :

{% block checkbox_row %}
{% spaceless %}
    <div>
        {% if not compound %}
            {% set label_attr = label_attr|merge({'for': id}) %}
        {% endif %}
        {% if required %}
            {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
        {% endif %}
        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}
        {{ form_widget(form) }}
        {% if label is empty %}
            {% set label = name|humanize %}
        {% endif %}
        </label>
        {{ form_errors(form) }}
    </div>
{% endspaceless %}
{% endblock checkbox_row %}

{% block form_label %}. , Zurb Foundation.

, {% block radio_row %}, , , Whistlegreg {% block choice_widget %} Symfony 2.1, {% block choice_widget_expanded %}. :

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {% set child_label = child.get('label') %}
        {% if child_label is not sameas(false) %}
            {% set child_id = child.get('id') %}
            {% set child_compound = child.get('compound') %}
            {% set child_required = child.get('required') %}
            {% set child_label_attr = child.get('label_attr') %}
            {% if not child_compound %}
                {% set child_label_attr = child_label_attr|merge({'for': child_id}) %}
            {% endif %}
            {% if child_required %}
                {% set child_label_attr = child_label_attr|merge({'class': (child_label_attr.class|default('') ~ ' required')|trim}) %}
            {% endif %}
            <label{% for attrname, attrvalue in child_label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
        {% endif %}
        {{ form_widget(child) }}
        {% if child_label is not sameas(false) %}
            {% if child_label is empty %}
                {% set child_label = name|humanize %}
            {% endif %}
            {{ child_label|trans({}, translation_domain) }}
            </label>
        {% endif %}
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

Symfony 2.1.9 DEV.

+1

, , form_row.

- :

{% block form_row %}
{% spaceless %}
    <label>
        {{ form_widget(form) }}
    </label>
{% endspaceless %}
{% endblock form_row %}

, ...
, , .
, form_row, .

0
source

All Articles