Overriding the form rendering template according t

2019-07-24 17:07发布

For example
I have two members in form object.

form_widget(form.icon)

form_widget(form.name)

I have changed 'choice_widget_expanded'

{% block choice_widget_expanded %}
{% spaceless %}
    <table {{ block('widget_container_attributes') }}>
    {% for child in form %}
      <tr>
        {{ form_widget(child) }}
        {{ form_label(child) }}
      </tr>
    {% endfor %}
    </table>
{% endspaceless %}
{% endblock choice_widget_expanded %}

However I would like to make it affect on {{form.icon}} only

is it possible ? how can I tell the object passed to this block is form.icon or form.name?

标签: forms symfony
1条回答
爷、活的狠高调
2楼-- · 2019-07-24 17:54

To override label block for choice_widget_expanded you can define your block and use it like in below

{% block choice_widget_expanded %}
{% spaceless %}
    <table {{ block('widget_container_attributes') }}>
    {% for child in form %}
      <tr>
        {{ form_widget(child) }}
        {{ form_label_custom(child) }}
      </tr>
    {% endfor %}
    </table>
{% endspaceless %}
{% endblock choice_widget_expanded %}

And for the custom label too form_label_custom

Note now for every choice field with expanded property (not all fields) your new label will be in action

{% block form_label_custom %}
{% spaceless %}
    {% if label is not sameas(false) %}
        {% if not compound %}
            {% set label_attr = label_attr|merge({'for': id}) %} /* you can skip this part for td */
        {% endif %}
        {% if required %}
            {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
        {% endif %}
        {% if label is empty %}
            {% set label = name|humanize %}
        {% endif %}
        <td {% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</td>
    {% endif %}
{% endspaceless %}
{% endblock form_label_custom %}

Or even more you can define the custom form_widget_custom(child) block to override like

{% block form_widget_custom %}
{% spaceless %}
    {% if compound %}
        {{ block('form_widget_compound') }}
    {% else %}
        {{ block('form_widget_simple') }}
    {% endif %}
{% endspaceless %}
{% endblock form_widget_custom %}

And now render your field

{{ form_widget_custom(form.icon) }}
查看更多
登录 后发表回答