@@ -30,13 +30,13 @@ going to need to build a form. But before you begin, let's focus on the generic
30
30
31
31
// src/Acme/StoreBundle/Entity/Product.php
32
32
namespace Acme\StoreBundle\Entity;
33
-
33
+
34
34
class Product
35
35
{
36
36
public $name;
37
-
37
+
38
38
protected $price;
39
-
39
+
40
40
public function getPrice()
41
41
{
42
42
return $this->price;
@@ -53,9 +53,9 @@ going to need to build a form. But before you begin, let's focus on the generic
53
53
If you're coding along with this example, be sure to create and enable
54
54
the ``AcmeStoreBundle ``. Run the following command and follow the on-screen
55
55
directions:
56
-
56
+
57
57
.. code-block :: text
58
-
58
+
59
59
php app/console init:bundle Acme/StoreBundle src/
60
60
61
61
This type of class is commonly called a "plain-old-PHP-object" because, so far,
@@ -134,17 +134,17 @@ helper functions:
134
134
.. code-block :: html+jinja
135
135
136
136
{# src/Acme/StoreBundle/Resources/views/Default/index.html.twig #}
137
-
137
+
138
138
<form action="{{ path('store_product') }}" method="post" {{ form_enctype(form) }}>
139
139
{{ form_widget(form) }}
140
-
140
+
141
141
<input type="submit" />
142
142
</form>
143
143
144
144
.. code-block :: html+php
145
-
10000
145
+
146
146
<?php // src/Acme/StoreBundle/Resources/views/Default/index.html.php ?>
147
-
147
+
148
148
<form action="<?php echo $view['router']->generate('store_product') ?>" method="post" <?php echo $view['form']->enctype($form) ?> >
149
149
<?php echo $view['form']->widget($form) ?>
150
150
@@ -187,7 +187,7 @@ controller:
187
187
{
188
188
// just setup a fresh $product object (no dummy data)
189
189
$product = new Product();
190
-
190
+
191
191
$form = $this->createFormBuilder($product)
192
192
->add('name', 'text')
193
193
->add('price', 'money', array('currency' => 'USD'))
@@ -203,7 +203,7 @@ controller:
203
203
return $this->redirect($this->generateUrl('store_product_success'));
204
204
}
205
205
}
206
-
206
+
207
207
// ...
208
208
}
209
209
@@ -221,7 +221,7 @@ of the ``$product`` object. This all happens via the ``bindRequest()`` method.
221
221
222
222
$product = new Product();
223
223
$product->name = 'Test product';
224
-
224
+
225
225
$form->bindRequest($this->get('request'));
226
226
echo $product->name;
227
227
@@ -328,7 +328,7 @@ number:
328
328
public static function loadValidatorMetadata(ClassMetadata $metadata)
329
329
{
330
330
$metadata->addPropertyConstraint('name', new NotBlank());
331
-
331
+
332
332
$metadata->addPropertyConstraint('price', new NotBlank());
333
333
$metadata->addPropertyConstraint('price', new Min(0));
334
334
}
@@ -423,10 +423,10 @@ guess (``text``).
423
423
424
424
The ``createFormBuilder() `` method takes up to two arguments neither
425
425
of which are required):
426
-
426
+
427
427
* The default data to initialize the form fields. This argument can be an
428
428
associative array or a plain old PHP object like in this example;
429
-
429
+
430
430
* an array of options for the form.
431
431
432
432
This example is pretty trivial, but field guessing can be a major time saver.
@@ -449,7 +449,7 @@ of code. Of course, you'll usually need much more flexibility when rendering:
449
449
.. code-block :: html+jinja
450
450
451
451
{# src/Acme/StoreBundle/Resources/views/Default/index.html.twig #}
452
-
452
+
453
453
<form action="{{ path('store_product') }}" method="post" {{ form_enctype(form) }}>
454
454
{{ form_errors(form) }}
455
455
@@ -462,9 +462,9 @@ of code. Of course, you'll usually need much more flexibility when rendering:
462
462
</form>
463
463
464
464
.. code-block :: html+php
465
-
465
+
466
466
<?php // src/Acme/StoreBundle/Resources/views/Default/index.html.php ?>
467
-
467
+
468
468
<form action="<?php echo $view['router']->generate('store_product') ?>" method="post" <?php echo $view['form']->enctype($form) ?>>
469
469
<?php echo $view['form']->errors($form) ?>
470
470
@@ -631,30 +631,30 @@ It can be used to quickly build a form object in the controller:
631
631
{
632
632
$product = // ...
633
633
$form = $this->createForm(new ProductType(), $product);
634
-
634
+
635
635
// ...
636
636
}
637
637
638
638
.. note ::
639
639
You can also set the data on the form via the ``setData() `` method:
640
-
640
+
641
641
.. code-block :: php
642
-
642
+
643
643
$form = $this->createForm(new ProductType());
644
644
F438
$form->setData($product);
645
645
646
646
If you use the ``setData `` method - and want to take advantage of field
647
647
type guessing, be sure to add the following to your form class:
648
-
648
+
649
649
.. code-block :: php
650
-
650
+
651
651
public function getDefaultOptions(array $options)
652
652
{
653
653
return array(
654
654
'data_class' => 'Acme\StoreBundle\Entity\Product',
655
655
);
656
656
}
657
-
657
+
658
658
This is necessary because the object is passed to the form after field
659
659
type guessing.
660
660
@@ -859,7 +859,7 @@ the following inside ``ProductType``:
859
859
// ...
860
860
861
861
$builder->add('reviews', 'collection', array(
862
- 'type' => new ProductReviewType(),
862
+ 'type' => new ProductReviewType(),
863
863
));
864
864
}
865
865
@@ -885,10 +885,10 @@ do this, create a new template file that will store the new markup:
885
885
.. configuration-block ::
886
886
887
887
.. code-block :: html+jinja
888
-
888
+
889
889
{# src/Acme/StoreBundle/Resources/views/Form/fields.html.twig #}
890
- {% extends 'TwigBundle :Form: div_layout.html.twig' %}
891
-
890
+ {% extends 'div_layout.html.twig' %}
891
+
892
892
{% block field_row %}
893
893
{% spaceless %}
894
894
<div class="form_row">
@@ -919,7 +919,7 @@ the form:
919
919
920
920
{# src/Acme/StoreBundle/Resources/views/Default/index.html.twig #}
921
921
{% form_theme form 'AcmeStoreBundle:Form: fields.html.twig' %}
922
-
922
+
923
923
<form ...>
924
924
925
925
The ``form_theme `` tag "imports" the template and uses all of its form-related
@@ -942,7 +942,7 @@ Form Template Blocks
942
942
Every part of a form that is rendered - HTML form elements, errors, labels, etc
943
943
- is defined in a base template as individual Twig blocks. By default, every
944
944
block needed is defined in the `div_layout.html.twig `_ file that lives inside
945
- the core `` TwigBundle `` . Inside this file, you can see every block needed
945
+ the ` Twig Bridge `_ . Inside this file, you can see every block needed
946
946
to render a form and every default field type.
947
947
948
948
Each block follows the same basic pattern and is broken up into two pieces,
@@ -978,14 +978,14 @@ a form that can be rendered:
978
978
By knowing the field type (e.g. ``textarea ``) and which part you want to
979
979
customize (e.g. ``widget ``), you can construct the block name that needs
980
980
to be overridden (e.g. ``textarea_widget ``). The best way to customize the
981
- block is to copy it from `` div_layout.html.twig `` to a new template, customize
981
+ block is to copy it from `div_layout.html.twig `_ to a new template, customize
982
982
it, and then use the ``form_theme `` tag as shown in the earlier example.
983
983
984
984
Form Type Block Inheritance
985
985
~~~~~~~~~~~~~~~~~~~~~~~~~~~
986
986
987
987
In some cases, the block you want to customize will appear to be missing.
988
- For example, if you look in the `` div_layout.html.twig `` file, you'll find
988
+ For example, if you look in the `div_layout.html.twig `_ file, you'll find
989
989
no ``textarea_errors `` block. So how are the errors for a textarea field
990
990
rendered?
991
991
@@ -1010,18 +1010,18 @@ templates in your application. To automatically include the customized blocks
1010
1010
from the ``fields.html.twig `` template created earlier, modify your application
1011
1011
configuration file:
1012
1012
1013
- .. configuration-block ::
1013
+ .. configuration-block ::
1014
1014
1015
1015
.. code-block :: yaml
1016
-
1016
+
1017
1017
# app/config/config.yml
1018
1018
twig :
1019
1019
form :
1020
1020
resources : ['AcmeStoreBundle:Form:fields.html.twig']
1021
1021
# ...
1022
-
1022
+
1023
1023
.. code-block :: xml
1024
-
1024
+
1025
1025
<!-- app/config/config.xml -->
1026
1026
<twig : config ...>
1027
1027
<twig : form >
@@ -1039,40 +1039,40 @@ configuration file:
1039
1039
));
1040
1040
1041
1041
Any blocks inside the ``fields.html.twig `` template are now used globally
1042
- to define form output.
1042
+ to define form output.
1043
1043
1044
1044
.. sidebar :: Customizing Form Output all in a Single File
1045
1045
1046
1046
You can also customize a form block right inside the template where that
1047
1047
customization is needed. Note that this method will only work if the
1048
1048
template used extends some base template via the ``{% extends %} ``:
1049
-
1049
+
1050
1050
.. code-block :: html+jinja
1051
-
1051
+
1052
1052
{% extends '::base.html.twig' %}
1053
-
1053
+
1054
1054
{% form_theme form _self %}
1055
- {% use 'TwigBundle :Form: div_layout.html.twig' %}
1055
+ {% use 'div_layout.html.twig' %}
1056
1056
1057
1057
{% block field_row %}
1058
1058
{# custom field row output #}
1059
1059
{% endblock field_row %}
1060
1060
1061
1061
{% block content %}
1062
1062
{# ... #}
1063
-
1063
+
1064
1064
{{ form_row(form.name) }}
1065
1065
{% endblock %}
1066
1066
1067
1067
The ``{% form_theme form _self %} `` tag allows form blocks to be customized
1068
1068
directly inside the template that will use those customizations. Use
1069
1069
this method to quickly make form output customizations that will only
1070
1070
ever be needed in a single template.
1071
-
1071
+
1072
1072
The ``use `` tag is also helpful as it gives you access to all of the
1073
- blocks defined inside `` div_layout.html.twig `` . For example, this ``use ``
1073
+ blocks defined inside `div_layout.html.twig `_ . For example, this ``use ``
1074
1074
statement is necessary to make the following form customization, as it
1075
- gives you access to the ``attributes `` block defined in `` div_layout.html.twig `` :
1075
+ gives you access to the ``attributes `` block defined in `div_layout.html.twig `_ :
1076
1076
1077
1077
.. code-block :: html+jinja
1078
1078
@@ -1158,5 +1158,6 @@ Learn more from the Cookbook
1158
1158
* :doc: `/cookbook/form/twig_form_customization `
1159
1159
1160
1160
.. _`Symfony2 Form Component` : https://github.com/symfony/Form
1161
- .. _`div_layout.html.twig` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/TwigBundle/Resources/views/Form/div_layout.html.twig
1161
+ .. _`Twig Bridge` : https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig
1162
+ .. _`div_layout.html.twig` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig
1162
1163
.. _`Cross-site request forgery` : http://en.wikipedia.org/wiki/Cross-site_request_forgery
0 commit comments