Skip to content

Instantly share code, notes, and snippets.

@jbinfo
Last active December 12, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbinfo/4731927 to your computer and use it in GitHub Desktop.
Save jbinfo/4731927 to your computer and use it in GitHub Desktop.
TWIG macro for display all Symfony2 form errors in your template
{#
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