Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize form_row for different form types

I want to set different form_row layout for different form type. I found in templates block named "choice_widget_collapsed", but it render only select tag with options.

I cannot find where this block is being used. Actually it seems that it is rendered instead of form_widget block. I suppose there is somewhere switch/if structure which checks form type and renders appropriate block, but i dont know where to find this switch, or dont know how to check input type inside form_row block.

I know that block type can be found inside form.vars.block_prefixes array, but this sux, because its position may change in the future as it was already.

So the question is: how can i make form_row display different thing depending on form field type?

like image 338
Fisher Avatar asked May 11 '26 00:05

Fisher


1 Answers

You should override the normal block in a theme of yours. This block should work like this:

{% block form_appropriate_block %}
    {% spaceless %}
        {% if form.vars.widget = 'myIntendedWidgetType' %}
            [yourTemplate]
        {% else %}
            {{ parent() }}
        {% endif %}
    {% endspaceless %}
{% endblock form_appropriate_block %}

And then, in your template, activate your theme using:

{% form_theme form 'MyBundle:Form:formTheme.html.twig' %}

In this way, your form theme is used only when needed, and if the type is not the one you want, it falls back to the normal behaviour.

like image 122
Alessandro Lai Avatar answered May 12 '26 13:05

Alessandro Lai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!