Symfony 2 Forms with Twig: Add a Form Variable to Existing Types

I need my symfony2 / twig forms to adhere to a certain condition: all lines of lines should look something like this:

{% block form_row %} <div class="CONSTANT_CLASS class_based_on_field_type class_based_on_error"> {{ form_label(form) }} {{ form_widget(form) }} ... </div> {% endblock form_row %} 

Please note that I need to get the field type in the form_row block. Alas, the type of field is determined only at the widget level.

I definitely need a way so that my form_row knows what type of field it is dealing with. Therefore, I suggest that it would be better to somehow override the form_row twig function.

Where can I override the default twin features? And how can this be done?

Remember that this is not about setting up a form . I need to know how to add existing form variables for existing field types.

@nifr: The key answer for you is {% set typeClass ... %} . But there is no text variable defined for the template. Look at form_div_layout.html.twig on line 158ff, I think the type is really set only at the level of form_widget and is thus closed to be there. This means that using the type at the form_row level will result in the given default value appearing (so this came to me during testing). If you can prove it wrong, I will gladly accept your answer.

0
source share
2 answers

How to override form_row block in Twig, adding attributes by field type?

Although you say that this is not about customizing the form, this can be achieved with this ... A brief introduction for others reading this now.

The default symfony extension form extensions can be found here .

The default twin theme theme can be found in Symfony / Bridge / Twig / Resources / views / Form / form_div_layout.html.twig .

General information on how to redefine forms can be found in the How to Make Customize Form Visualization chapter in the book, but I will briefly sign it.

form_row Default

 {% block form_row %} {% spaceless %} <div> {{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endspaceless %} {% endblock form_row %} 

Form Level Override

Add this to the form template you want to customize:

 {% form_theme form _self %} 

If you want to put {% block form_row%} in another package / template, use this:

 {% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %} 

Now insert your own form_row block after the form_theme declaration or place it in the specified template (in our case it will be AcmeDemoBundle: Form: fields.html.twig).

In my example, we will add the error class if there is an error in the form line and the other class name is the type name of the current field type.

 {% block form_row %} {% spaceless %} {# set class to 'error' if errors exist #} {% set attr = attr|merge({'class': attr.class|default('') ~ (errors|length > 0 ? ' error' : '') }) %} {% set typeClass = ' ' ~ type|default('text') %} {# you could also implement a logic matching input types with an array of their desired classname-representations here. #} {% set attr = attr|merge({'class': attr.class|default('') ~ type) }) %} <div class="{% for class in attr.class %}{{ class }}{% endfor %}{{ typeClass }}"> {{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endspaceless %} {% endblock form_row %} 

if you want to apply your form_row block throughout the system, add your AcmeDemoBundle: Form: fields.html.twig to your twig.templating.form.resources!

 # app/config/config.yml framework: templating: form: resources: - 'AcmeDemoBundle:Form' 
0
source

In the form_row block you can use:

{{form.vars.block_prefixes [2]}}

form.vars.block_prefixes gives you an array with additional information and may vary with versions. But from what I have seen so far, there is always index 2

0
source

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


All Articles