If you are new to Symfony2, you might come to a point where you want to customize the form error class. By default, all the form errors will be shown using a list which does not have any css class assigned to it.
To make the error message stand out, i need to add a css class to it and style the form error to be red color. Thanks to twig template, where i can easily extend the template to what i need.
How to customize form error in Symfony 2.6?
Advertisements
- Create a new file at src/Acme/DemoBundle/Resources/views/Form/form_errors.html.twig with the content below:-
{% block form_errors -%} {% if errors|length > 0 -%} <ul class="error_list"> {%- for error in errors -%} <li>{{ error.message }}</li> {%- endfor -%} </ul> {%- endif %} {%- endblock form_errors %}
- Once save, now we need to tell twig to load your template whenever form is load.
So, go to app/config.yml, look for twig section and add the form and resources lines like below:-twig: form: resources: [ 'AcmeDemoBundle:Form:form_errors.html.twig' ]
- Voila. whenever form is load, your customize form error will be loaded too!
* If you want to find out more about customizing form error using other way than twig, you can read this.
Related posts:
How to convert doc file to pdf file in Win XP - PDFCreator
How to change default mail client for Ms Excel 2007?
How to set out of office auto responder in Zimbra
Giveaway: Youtube video download and converter for Mac - Worth $29
Free 5 Licence keys for iOrgsoft PDF to Word Converter
How to archive email in Ms Outlook
WordPress: How to create left sidebar template in TwentyEleven theme?
Prestashop 1.6: "Unexpected token <" error when upload category thumbnail
Share this with your friends:-
Update in Symfony3 its:
twig.form_themes
source:
http://symfony.com/doc/current/reference/configuration/twig.html#main