Customizing Symfony Form Templates

From Littledamien Wiki
Revision as of 15:06, 8 February 2015 by Video8 (talk | contribs)
Jump to navigation Jump to search


Form errors

Form errors are rendered with a {% form_errors %} block.

Field errors are rendered with a {% field_errors %} block.

Global customization

Create a stand-along template that overrides the form_errors block:[1]

{# app/Resources/views/form_theme.html.twig #}

{% block form_errors %}
	<div class="alert alert-danger alert-form-errors">
{% for error in errors %}
		<div>{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</div>
{% endfor %}
	</div>
{% endblock %}

Update the application configuration:

# app/config/config.yml

# ...

twig:
	form_themes:
		- ':form:errors.html.twig'

Notes

  1. Making Application-Wide Customizations, Symfony Template Cookbook