4
4
How to Dynamically Generate Forms Using Form Events
5
5
===================================================
6
6
7
- Before jumping right into dynamic form generation, let's have a quick review
7
+ Before jumping right into dynamic form generation, let's have a quick review
8
8
of what a bare form class looks like::
9
9
10
10
// src/Acme/DemoBundle/Form/Type/ProductType.php
11
11
namespace Acme\DemoBundle\Form\Type;
12
12
13
13
use Symfony\Component\Form\AbstractType;
14
14
use Symfony\Component\Form\FormBuilder;
15
-
15
+
16
16
class ProductType extends AbstractType
17
17
{
18
18
public function buildForm(FormBuilder $builder, array $options)
@@ -29,28 +29,28 @@ of what a bare form class looks like::
29
29
30
30
.. note ::
31
31
32
- If this particular section of code isn't already familiar to you, you
33
- probably need to take a step back and first review the :doc: `Forms chapter </book/forms >`
32
+ If this particular section of code isn't already familiar to you, you
33
+ probably need to take a step back and first review the :doc: `Forms chapter </book/forms >`
34
34
before proceeding.
35
35
36
36
Let's assume for a moment that this form utilizes an imaginary "Product" class
37
- that has only two relevant properties ("name" and "price"). The form generated
37
+ that has only two relevant properties ("name" and "price"). The form generated
38
38
from this class will look the exact same regardless of a new Product is being created
39
39
or if an existing product is being edited (e.g. a product fetched from the database).
40
40
41
- Suppose now, that you don't want the user to be able to change the ``name `` value
41
+ Suppose now, that you don't want the user to be able to change the ``name `` value
42
42
once the object has been created. To do this, you can rely on Symfony's :doc: `Event Dispatcher </components/event_dispatcher/introduction >`
43
- system to analyze the data on the object and modify the form based on the
44
- Product object's data. In this entry, you'll learn how to add this level of
43
+ system to analyze the data on the object and modify the form based on the
44
+ Product object's data. In this entry, you'll learn how to add this level of
45
45
flexibility to your forms.
46
46
47
47
.. _`cookbook-forms-event-subscriber` :
48
48
49
49
Adding An Event Subscriber To A Form Class
50
50
------------------------------------------
51
51
52
- So, instead of directly adding that "name" widget via our ProductType form
53
- class, let's delegate the responsibility of creating that particular field
52
+ So, instead of directly adding that "name" widget via our ProductType form
53
+ class, let's delegate the responsibility of creating that particular field
54
54
to an Event Subscriber::
55
55
56
56
// src/Acme/DemoBundle/Form/Type/ProductType.php
@@ -75,8 +75,8 @@ to an Event Subscriber::
75
75
}
76
76
}
77
77
78
- The event subscriber is passed the FormFactory object in its constructor so
79
- that our new subscriber is capable of creating the form widget once it is
78
+ The event subscriber is passed the FormFactory object in its constructor so
79
+ that our new subscriber is capable of creating the form widget once it is
80
80
notified of the dispatched event during form creation.
81
81
82
82
.. _`cookbook-forms-inside-subscriber-class` :
@@ -119,8 +119,8 @@ might look like the following::
119
119
120
120
// During form creation setData() is called with null as an argument
121
121
// by the FormBuilder constructor. We're only concerned with when
122
- // setData is called with an actual Entity object in it (whether new,
123
- // or fetched with Doctrine). This if statement let's us skip right
122
+ // setData is called with an actual Entity object in it (whether new
123
+ // or fetched with Doctrine). This if statement lets us skip right
124
124
// over the null condition.
125
125
if (null === $data) {
126
126
return;
@@ -135,27 +135,27 @@ might look like the following::
135
135
136
136
.. caution ::
137
137
138
- It is easy to misunderstand the purpose of the ``if (null === $data) `` segment
139
- of this event subscriber. To fully understand its role, you might consider
140
- also taking a look at the `Form class `_ and paying special attention to
141
- where setData() is called at the end of the constructor, as well as the
138
+ It is easy to misunderstand the purpose of the ``if (null === $data) `` segment
139
+ of this event subscriber. To fully understand its role, you might consider
140
+ also taking a look at the `Form class `_ and paying special attention to
141
+ where setData() is called at the end of the constructor, as well as the
142
142
setData() method itself.
143
143
144
- The ``FormEvents::PRE_SET_DATA `` line actually resolves to the string ``form.pre_set_data ``.
144
+ The ``FormEvents::PRE_SET_DATA `` line actually resolves to the string ``form.pre_set_data ``.
145
145
The `FormEvents class `_ serves an organizational purpose. It is a centralized location
146
146
in which you can find all of the various form events available.
147
147
148
- While this example could have used the ``form.set_data `` event or even the ``form.post_set_data ``
149
- events just as effectively, by using ``form.pre_set_data `` we guarantee that
150
- the data being retrieved from the ``Event `` object has in no way been modified
151
- by any other subscribers or listeners. This is because ``form.pre_set_data ``
152
- passes a `DataEvent `_ object instead of the `FilterDataEvent `_ object passed
153
- by the ``form.set_data `` event. `DataEvent `_, unlike its child `FilterDataEvent `_,
148
+ While this example could have used the ``form.set_data `` event or even the ``form.post_set_data ``
149
+ events just as effectively, by using ``form.pre_set_data `` we guarantee that
150
+ the data being retrieved from the ``Event `` object has in no way been modified
151
+ by any other subscribers or listeners. This is because ``form.pre_set_data ``
152
+ passes a `DataEvent `_ object instead of the `FilterDataEvent `_ object passed
153
+ by the ``form.set_data `` event. `DataEvent `_, unlike its child `FilterDataEvent `_,
154
154
lacks a setData() method.
155
155
156
156
.. note ::
157
157
158
- You may view the full list of form events via the `FormEvents class `_,
158
+ You may view the full list of form events via the `FormEvents class `_,
159
159
found in the form bundle.
160
160
161
161
.. _`DataEvent` : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Event/DataEvent.php
0 commit comments