Customizing Symfony Form Templates: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
[[Category:Symfony]] [[Category:Web Development]] | [[Category:Symfony]] [[Category:Web Development]] | ||
== | == Application-wide customization of form themes == | ||
The application-wide setting to use a different built-in theme to render forms: | |||
<syntaxhighlight lang="yaml"> | |||
# app/config/config.yml | |||
twig: | |||
form_themes: | |||
- 'bootstrap_3_layout.html.twig' | |||
</syntaxhighlight> | |||
To implement a custom theme application-wide: | |||
= | <syntaxhighlight lang="yaml"> | ||
# app/config/config.yml | |||
twig: | |||
form_themes: | |||
- ':form:errors.html.twig' | |||
</syntaxhighlight> | |||
Create a stand-along template that overrides the `form_errors` block:<ref>[http://symfony.com/doc/current/cookbook/form/form_customization.html#making-application-wide-customizations Making Application-Wide Customizations], Symfony Template Cookbook</ref> | Create a stand-along template that overrides the `form_errors` block:<ref>[http://symfony.com/doc/current/cookbook/form/form_customization.html#making-application-wide-customizations Making Application-Wide Customizations], Symfony Template Cookbook</ref> | ||
Update the application configuration: | Update the application configuration: | ||
| Line 33: | Line 33: | ||
form_themes: | form_themes: | ||
- ':form:errors.html.twig' | - ':form:errors.html.twig' | ||
</syntaxhighlight> | |||
== Form errors == | |||
Form errors are rendered with a `{% form_errors %}` block. | |||
Field errors are rendered with a `{% field_errors %}` block. | |||
=== Custom form errors theme === | |||
<syntaxhighlight lang="twig"> | |||
{# 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 %} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Notes == | == Notes == | ||
<references /> | <references /> | ||
Revision as of 18:17, 13 February 2015
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]
Update the application configuration:
# app/config/config.yml # ... twig: form_themes: - ':form:errors.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