Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 customize form error messages

can you please help me understand how to customize error messages in a form with Symfony2? I want to change the HTML layout, adding div, class, etc...

Reading the guide, it gives a piece of code to put in a file called fields_errors.html.twig but it doesn't tells where to put this file and if some extra configuration is needed.

Can somebody help me?

like image 205
Bagbyte Avatar asked Aug 25 '12 10:08

Bagbyte


2 Answers

You have to put the template in Resourses/views/ folder of your bundle. For example,

{# Vendor/YourBundle/Resourses/views/form_fields.html.twig #}

{% extends 'form_div_layout.html.twig' %}

{% block form_errors %}
    {# your form error template #}
{% endblock form_errors %}

{# other customized blocks #}

And then in your form page template,

{% extends your:page:layout %}

{% form_theme form  'VendorYourBundle::form_fields.html.twig' %}

 {{ form_errors(form.field) }}
 {# ..... #}

For more option and implementation reference check form theme cookbook entry and default field layout implementation

like image 197
Mun Mun Das Avatar answered Oct 27 '22 16:10

Mun Mun Das


You can customise all your error messages at once in your template:

<div class="your_new_class">
    {{ form_errors(form) }}
</div>

Or individually (if your field is title for example)

<div class="your_new_class">
    {{ form_errors(form.task) }}
</div>
like image 43
Mick Avatar answered Oct 27 '22 17:10

Mick