Customizing Symfony Form Templates
Application-wide customization of form themes
The application-wide setting to use a different built-in theme to render forms:
# app/config/config.yml twig: form_themes: - 'bootstrap_3_layout.html.twig'
To implement a custom theme application-wide:
# app/config/config.yml twig: form_themes: - ':form:errors.html.twig'
Create a stand-along template that overrides the form_errors block:[1]
N.B. when combining a built-in theme with local form theme customizations, it's necessary to put the local customization after the built-in, e.g.:
# app/config/config.yml form_themes: - 'bootstrap_3_layout.html.twig' - ':form:errors.html.twig' - ':form:fields.html.twig'
Form errors
Form errors are rendered with a {% form_errors %} block.
Field errors are rendered with a {% field_errors %} block.
Custom form errors theme
{# app/Resources/views/form/errors.html.twig #}
{% block form_errors %}
{% if errors.count > 0 %}
<div class="alert alert-danger alert-form-errors">
{% for error in errors %}
<div>{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</div>
{% endfor %}
</div>
{% endif %}
{% endblock %}
Notes
- ↑ Making Application-Wide Customizations, Symfony Template Cookbook