Last active
December 12, 2015 06:49
-
-
Save jbinfo/4731927 to your computer and use it in GitHub Desktop.
TWIG macro for display all Symfony2 form errors in your template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
this macro is for display all form errors(globals, fields errors) | |
@param is_global have two values 1 and 2 | |
1 => for a global error | |
2 => for a field error | |
#} | |
{% macro all_form_errors(form, is_global) %} | |
{% set is_global = is_global|default(1) %} | |
{% for error in form.get('errors') %} | |
<span style="display: block"> | |
{% if is_global == 1 %} | |
{{ error.messageTemplate|trans(error.messageParameters, 'validators') }} | |
{% else %} | |
{{ form_label(form)|striptags|trim }} => {{ form_errors(form)|striptags }} | |
{% endif %} | |
</span> | |
{% endfor %} | |
{% if attribute(form, 'hasChildren') is sameas(true) %} | |
{% for child in attribute(form, 'getChildren') %} | |
{{ _self.all_form_errors(child, 2) }} | |
{% endfor %} | |
{% endif %} | |
{% endmacro %} | |
{# Run #} | |
{{ _self.all_form_errors(form) }} | |
{# | |
You can personalize it to your needs, | |
like to specify the translation domain for globals errors(validators). | |
Enjoy! | |
#} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment