@@ -22,8 +22,11 @@ Simple Example: Sanitizing HTML on User Input
22
22
Suppose you have a Task form with a description ``textarea `` type::
23
23
24
24
// src/AppBundle/Form/TaskType.php
25
- // ...
26
25
26
+ use Symfony\Component\Form\FormBuilderInterface;
27
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
28
+
29
+ // ...
27
30
class TaskType extends AbstractType
28
31
{
29
32
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -45,7 +48,7 @@ Suppose you have a Task form with a description ``textarea`` type::
45
48
But, there are two complications:
46
49
47
50
#. Your users are allowed to use *some * HTML tags, but not others: you need a way
48
- to call :phpfunction: `striptags ` after the form is submitted.
51
+ to call :phpfunction: `striptags ` after the form is submitted;
49
52
50
53
#. To be friendly, you want to convert ``<br/> `` tags into line breaks (``\n ``) before
51
54
rendering the field so the text is easier to edit.
@@ -108,8 +111,8 @@ issue number.
108
111
Start by setting up the text field like normal::
109
112
110
113
// src/AppBundle/Form/TaskType.php
111
- // ...
112
114
115
+ // ...
113
116
class TaskType extends AbstractType
114
117
{
115
118
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -134,7 +137,7 @@ property would be a string (e.g. "55"). How can you transform this into an ``Iss
134
137
entity on submit?
135
138
136
139
Creating the Transformer
137
- ------------------------
140
+ ~~~~~~~~~~~~~~~~~~~~~~~~
138
141
139
142
You could use the ``CallbackTransformer `` like earlier. But since this is a bit more
140
143
complex, creating a new transformer class will keep the ``TaskType `` form class simpler.
@@ -146,17 +149,17 @@ to and from the issue number and the ``Issue`` object::
146
149
namespace AppBundle\Form\DataTransformer;
147
150
148
151
use AppBundle\Entity\Issue;
149
- use Doctrine\Common\Persistence\ObjectManager ;
152
+ use Doctrine\Common\Persistence\EntityManager ;
150
153
use Symfony\Component\Form\DataTransformerInterface;
151
154
use Symfony\Component\Form\Exception\TransformationFailedException;
152
155
153
156
class IssueToNumberTransformer implements DataTransformerInterface
154
157
{
155
- private $em ;
158
+ private $entityManager ;
156
159
157
- public function __construct(ObjectManager $em )
160
+ public function __construct(EntityManager $entityManager )
158
161
{
159
- $this->em = $em ;
162
+ $this->entityManager = $entityManager ;
160
163
}
161
164
162
165
/**
@@ -185,10 +188,10 @@ to and from the issue number and the ``Issue`` object::
185
188
{
186
189
// no issue number? It's optional, so that's ok
187
190
if (!$issueNumber) {
188
- return null ;
191
+ return;
189
192
}
190
193
191
- $issue = $this->em
194
+ $issue = $this->entityManager
192
195
->getRepository('AppBundle:Issue')
193
196
// query for the issue with this id
194
197
->find($issueNumber)
@@ -225,7 +228,7 @@ that message with the ``invalid_message`` option (see below).
225
228
an empty string, 0 for integers or 0.0 for floats).
226
229
227
230
Using the Transformer
228
- ---------------------
231
+ ~~~~~~~~~~~~~~~~~~~~~
229
232
230
233
Next, you need to instantiate the ``IssueToNumberTransformer `` class from inside
231
234
``TaskType `` and add it to the ``issue `` field. But to do that, you'll need an instance
@@ -235,16 +238,18 @@ No problem! Just add a ``__construct()`` function to ``TaskType`` and force this
235
238
to be passed in. Then, you can easily create and add the transformer::
236
239
237
240
// src/AppBundle/Form/TaskType.php
241
+
238
242
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
239
- use Doctrine\Common\Persistence\ObjectManager ;
243
+ use Doctrine\Common\Persistence\EntityManager ;
240
244
245
+ // ...
241
246
class TaskType extends AbstractType
242
247
{
243
- private $em ;
248
+ private $entityManager ;
244
249
245
- public function __construct(ObjectManager $em )
250
+ public function __construct(EntityManager $entityManager )
246
251
{
247
- $this->em = $em ;
252
+ $this->entityManager = $entityManager ;
248
253
}
249
254
250
255
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -259,7 +264,7 @@ to be passed in. Then, you can easily create and add the transformer::
259
264
// ...
260
265
261
266
$builder->get('issue')
262
- ->addModelTransformer(new IssueToNumberTransformer($this->em ));
267
+ ->addModelTransformer(new IssueToNumberTransformer($this->entityManager ));
263
268
}
264
269
265
270
// ...
@@ -268,8 +273,8 @@ to be passed in. Then, you can easily create and add the transformer::
268
273
Now, when you create your ``TaskType ``, you'll need to pass in the entity manager::
269
274
270
275
// e.g. in a controller somewhere
271
- $em = $this->getDoctrine()->getManager();
272
- $form = $this->createForm(new TaskType($em ), $task);
276
+ $entityManager = $this->getDoctrine()->getManager();
277
+ $form = $this->createForm(new TaskType($entityManager ), $task);
273
278
274
279
// ...
275
280
@@ -312,23 +317,23 @@ First, create the custom field type class::
312
317
namespace AppBundle\Form;
313
318
314
319
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
315
- use Doctrine\Common\Persistence\ObjectManager ;
320
+ use Doctrine\ORM\EntityManager ;
316
321
use Symfony\Component\Form\AbstractType;
317
322
use Symfony\Component\Form\FormBuilderInterface;
318
323
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
319
324
320
325
class IssueSelectorType extends AbstractType
321
326
{
322
- private $em ;
327
+ private $entityManager ;
323
328
324
- public function __construct(ObjectManager $em )
329
+ public function __construct(EntityManager $entityManager )
325
330
{
326
- $this->em = $em ;
331
+ $this->entityManager = $entityManager ;
327
332
}
328
333
329
334
public function buildForm(FormBuilderInterface $builder, array $options)
330
335
{
331
- $transformer = new IssueToNumberTransformer($this->em );
336
+ $transformer = new IssueToNumberTransformer($this->entityManager );
332
337
$builder->addModelTransformer($transformer);
333
338
}
334
339
@@ -423,15 +428,15 @@ types of underlying data.
423
428
424
429
In any form, the three different types of data are:
425
430
426
- 1) **Model data ** - This is the data in the format used in your application
427
- (e.g. an ``Issue `` object). If you call ``Form::getData `` or ``Form::setData ``,
431
+ #. **Model data ** - This is the data in the format used in your application
432
+ (e.g. an ``Issue `` object). If you call ``Form::getData() `` or ``Form::setData() ``,
428
433
you're dealing with the "model" data.
429
434
430
- 2) **Norm Data ** - This is a normalized version of your data, and is commonly
435
+ #. **Norm Data ** - This is a normalized version of your data and is commonly
431
436
the same as your "model" data (though not in our example). It's not commonly
432
437
used directly.
433
438
434
- 3) **View Data ** - This is the format that's used to fill in the form fields
439
+ #. **View Data ** - This is the format that's used to fill in the form fields
435
440
themselves. It's also the format in which the user will submit the data. When
436
441
you call ``Form::submit($data) ``, the ``$data `` is in the "view" data format.
437
442
@@ -463,3 +468,7 @@ The difference between the transformers is subtle and you should always think
463
468
about what the "norm" data for a field should really be. For example, the
464
469
"norm" data for a ``text `` field is a string, but is a ``DateTime `` object
465
470
for a ``date `` field.
471
+
472
+ .. tip ::
473
+
474
+ As a general rule, the normalized data should contain as much information as possible.
0 commit comments