Customizing Symfony Form Templates: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by 2 users not shown)
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:
''(Not certain of the minimum Symfony version that supports this, but 2.3 does not and [[Installing_and_Configuring_Symfony2_on_Windows#Upgrading|2.6 does]].)''
<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>
''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.:
<syntaxhighlight lang="yaml">
# app/config/config.yml
form_themes:
- 'bootstrap_3_layout.html.twig'
- ':form:errors.html.twig'
- ':form:fields.html.twig'
</syntaxhighlight>


== Form errors ==
== Form errors ==
Line 7: Line 41:
Field errors are rendered with a `{% field_errors %}` block.
Field errors are rendered with a `{% field_errors %}` block.


=== Global customization ===
=== Custom form errors theme ===
 
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>


<syntaxhighlight lang="twig">
<syntaxhighlight lang="twig">
{# app/Resources/views/form_theme.html.twig #}
{# app/Resources/views/form/errors.html.twig #}
 
{% block form_errors %}
{% block form_errors %}
{% if errors.count > 0 %}
<div class="alert alert-danger alert-form-errors">
<div class="alert alert-danger alert-form-errors">
{% for error in errors %}
{% for error in errors %}
Line 20: Line 52:
{% endfor %}
{% endfor %}
</div>
</div>
{% endif %}
{% endblock %}
{% endblock %}
</syntaxhighlight>
Update the application configuration:
<syntaxhighlight lang="yaml">
# app/config/config.yml
# ...
twig:
form_themes:
- ':form:errors.html.twig'
</syntaxhighlight>
</syntaxhighlight>


== Notes ==
== Notes ==
<references />
<references />

Latest revision as of 17:10, 15 February 2015


Application-wide customization of form themes[edit]

The application-wide setting to use a different built-in theme to render forms:

(Not certain of the minimum Symfony version that supports this, but 2.3 does not and 2.6 does.)

# 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[edit]

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

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

Custom form errors theme[edit]

{# 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[edit]

  1. Making Application-Wide Customizations, Symfony Template Cookbook