1
1
How to customize Form Rendering
2
2
===============================
3
3
4
- Symfony gives you a wide variety of ways to customize the form rendering. In this
5
- guide, you'll learn how to customize every possible part of your form with as
6
- little effort as possible whether you use Twig or PHP as the templating engine.
4
+ Symfony gives you a wide variety of ways to customize how a form is rendered.
5
+ In this guide, you'll learn how to customize every possible part of your
6
+ form with as little effort as possible whether you use Twig or PHP as your
7
+ templating engine.
7
8
8
9
Form Rendering Basics
9
10
---------------------
10
11
11
12
Recall that the label, error and HTML widget of a form field can easily
12
- be rendered by using the ``form_row `` Twig function or the ``row `` helper
13
+ be rendered by using the ``form_row `` Twig function or the ``row `` PHP helper
13
14
method:
14
15
15
16
.. code-block :: jinja
@@ -70,18 +71,23 @@ rendering in general, see :ref:`form-rendering-template`.
70
71
What are Form Themes?
71
72
---------------------
72
73
73
- Symfony uses fragments which are markup templates for each and every part of a
74
- form - field labels, errors, `` input `` text fields, `` select `` tags, etc - when
75
- rendering a form.
74
+ Symfony uses form fragments - a small piece of a template that renders just
75
+ one part of a form - to render every part of a form - - field labels, errors,
76
+ `` input `` text fields, `` select `` tags, etc
76
77
77
78
The fragments are defined as blocks in Twig and as template files in PHP.
78
79
79
- A theme is no more than a set of fragments to use when rendering a form. Symfony
80
- comes with a default theme (`form_div_layout.html.twig `_ in Twig and ``FrameworkBundle:Form ``
81
- in PHP).
80
+ A *theme * is nothing more than a set of fragments that you want to use when
81
+ rendering a form. In other words, if you want to customize one portion of
82
+ how a form is rendered, you'll import a *theme * which contains a customization
83
+ of the appropriate form fragments.
82
84
83
- In the next section you will learn how to customize a theme by overriding some or
84
- all of the fragments in a base theme.
85
+ Symfony comes with a default theme (`form_div_layout.html.twig `_ in Twig and
86
+ ``FrameworkBundle:Form `` in PHP) that defines each and every fragment needed
87
+ to render every part of a form.
88
+
89
+ In the next section you will learn how to customize a theme by overriding
90
+ some or all of its fragments.
85
91
86
92
For example, when the widget of a ``integer `` type field is rendered, an ``input ``
87
93
``number `` field is generated
@@ -146,7 +152,7 @@ As you can see, this fragment itself renders another fragment - ``field_widget``
146
152
147
153
The point is, the fragments dictate the HTML output of each part of a form. To
148
154
customize the form output, you just need to identify and override the correct
149
- fragment. A set of these form fragment customizations is known as a from "theme".
155
+ fragment. A set of these form fragment customizations is known as a form "theme".
150
156
When rendering a form, you can choose which form theme(s) you want to apply.
151
157
152
158
In Twig a theme is a single template file and the fragments are the blocks defined
@@ -281,7 +287,7 @@ block from the new template and the ``input`` tag will be wrapped in the
281
287
Form Theming in PHP
282
288
-------------------
283
289
284
- When using PHP as a templating engine the only method to customize a fragment
290
+ When using PHP as a templating engine, the only method to customize a fragment
285
291
is to create a new template file - this is similar to the second method used by
286
292
Twig.
287
293
@@ -378,7 +384,7 @@ Making Application-wide Customizations
378
384
--------------------------------------
379
385
380
386
If you'd like a certain form customization to be global to your application,
381
- you can accomplish this by making the form customizations to an external
387
+ you can accomplish this by making the form customizations in an external
382
388
template and then importing it inside your application configuration:
383
389
384
390
Twig
@@ -397,7 +403,6 @@ form is rendered.
397
403
twig :
398
404
form :
399
405
resources :
400
- - ' form_div_layout.html.twig'
401
406
- ' AcmeDemoBundle:Form:fields.html.twig'
402
407
# ...
403
408
@@ -407,7 +412,6 @@ form is rendered.
407
412
408
413
<twig : config ...>
409
414
<twig : form >
410
- <resource >form_div_layout.html.twig</resource >
411
415
<resource >AcmeDemoBundle:Form:fields.html.twig</resource >
412
416
</twig : form >
413
417
<!-- ... -->
@@ -419,7 +423,6 @@ form is rendered.
419
423
420
424
$container->loadFromExtension('twig', array(
421
425
'form' => array('resources' => array(
422
- 'form_div_layout.html.twig',
423
426
'AcmeDemoBundle:Form:fields.html.twig',
424
427
))
425
428
// ...
@@ -489,7 +492,6 @@ form is rendered.
489
492
templating :
490
493
form :
491
494
resources :
492
- - ' FrameworkBundle:Form'
493
495
- ' AcmeDemoBundle:Form'
494
496
# ...
495
497
@@ -501,7 +503,6 @@ form is rendered.
501
503
<framework : config ...>
502
504
<framework : templating >
503
505
<framework : form >
504
- <resource >FrameworkBundle:Form</resource >
505
506
<resource >AcmeDemoBundle:Form</resource >
506
507
</framework : form >
507
508
</framework : templating >
@@ -517,7 +518,6 @@ form is rendered.
517
518
$container->loadFromExtension('framework', array(
518
519
'templating' => array('form' =>
519
520
array('resources' => array(
520
- 'FrameworkBundle:Form',
521
521
'AcmeDemoBundle:Form',
522
522
)))
523
523
// ...
@@ -614,6 +614,7 @@ Here, the ``_product_name_widget`` fragment defines the template to use for the
614
614
field whose *id * is ``product_name `` (and name is ``product[name] ``).
615
615
616
616
.. tip ::
617
+
617
618
The ``product `` portion of the field is the form name, which may be set
618
619
manually or generated automatically based on your form type name (e.g.
619
620
``ProductType `` equates to ``product ``). If you're not sure what your
0 commit comments