Skip to content

Commit 9be07f0

Browse files
committed
Many tweaks thanks to review
1 parent b48feda commit 9be07f0

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

cookbook/form/data_transformers.rst

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ Simple Example: Sanitizing HTML on User Input
2222
Suppose you have a Task form with a description ``textarea`` type::
2323

2424
// src/AppBundle/Form/TaskType.php
25-
// ...
2625

26+
use Symfony\Component\Form\FormBuilderInterface;
27+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
28+
29+
// ...
2730
class TaskType extends AbstractType
2831
{
2932
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -45,7 +48,7 @@ Suppose you have a Task form with a description ``textarea`` type::
4548
But, there are two complications:
4649

4750
#. 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;
4952

5053
#. To be friendly, you want to convert ``<br/>`` tags into line breaks (``\n``) before
5154
rendering the field so the text is easier to edit.
@@ -108,8 +111,8 @@ issue number.
108111
Start by setting up the text field like normal::
109112

110113
// src/AppBundle/Form/TaskType.php
111-
// ...
112114

115+
// ...
113116
class TaskType extends AbstractType
114117
{
115118
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
134137
entity on submit?
135138

136139
Creating the Transformer
137-
------------------------
140+
~~~~~~~~~~~~~~~~~~~~~~~~
138141

139142
You could use the ``CallbackTransformer`` like earlier. But since this is a bit more
140143
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::
146149
namespace AppBundle\Form\DataTransformer;
147150

148151
use AppBundle\Entity\Issue;
149-
use Doctrine\Common\Persistence\ObjectManager;
152+
use Doctrine\Common\Persistence\EntityManager;
150153
use Symfony\Component\Form\DataTransformerInterface;
151154
use Symfony\Component\Form\Exception\TransformationFailedException;
152155

153156
class IssueToNumberTransformer implements DataTransformerInterface
154157
{
155-
private $em;
158+
private $entityManager;
156159

157-
public function __construct(ObjectManager $em)
160+
public function __construct(EntityManager $entityManager)
158161
{
159-
$this->em = $em;
162+
$this->entityManager = $entityManager;
160163
}
161164

162165
/**
@@ -185,10 +188,10 @@ to and from the issue number and the ``Issue`` object::
185188
{
186189
// no issue number? It's optional, so that's ok
187190
if (!$issueNumber) {
188-
return null;
191+
return;
189192
}
190193

191-
$issue = $this->em
194+
$issue = $this->entityManager
192195
->getRepository('AppBundle:Issue')
193196
// query for the issue with this id
194197
->find($issueNumber)
@@ -225,7 +228,7 @@ that message with the ``invalid_message`` option (see below).
225228
an empty string, 0 for integers or 0.0 for floats).
226229

227230
Using the Transformer
228-
---------------------
231+
~~~~~~~~~~~~~~~~~~~~~
229232

230233
Next, you need to instantiate the ``IssueToNumberTransformer`` class from inside
231234
``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
235238
to be passed in. Then, you can easily create and add the transformer::
236239

237240
// src/AppBundle/Form/TaskType.php
241+
238242
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
239-
use Doctrine\Common\Persistence\ObjectManager;
243+
use Doctrine\Common\Persistence\EntityManager;
240244

245+
// ...
241246
class TaskType extends AbstractType
242247
{
243-
private $em;
248+
private $entityManager;
244249

245-
public function __construct(ObjectManager $em)
250+
public function __construct(EntityManager $entityManager)
246251
{
247-
$this->em = $em;
252+
$this->entityManager = $entityManager;
248253
}
249254

250255
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -259,7 +264,7 @@ to be passed in. Then, you can easily create and add the transformer::
259264
// ...
260265

261266
$builder->get('issue')
262-
->addModelTransformer(new IssueToNumberTransformer($this->em));
267+
->addModelTransformer(new IssueToNumberTransformer($this->entityManager));
263268
}
264269

265270
// ...
@@ -268,8 +273,8 @@ to be passed in. Then, you can easily create and add the transformer::
268273
Now, when you create your ``TaskType``, you'll need to pass in the entity manager::
269274

270275
// 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);
273278

274279
// ...
275280

@@ -312,23 +317,23 @@ First, create the custom field type class::
312317
namespace AppBundle\Form;
313318

314319
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
315-
use Doctrine\Common\Persistence\ObjectManager;
320+
use Doctrine\ORM\EntityManager;
316321
use Symfony\Component\Form\AbstractType;
317322
use Symfony\Component\Form\FormBuilderInterface;
318323
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
319324

320325
class IssueSelectorType extends AbstractType
321326
{
322-
private $em;
327+
private $entityManager;
323328

324-
public function __construct(ObjectManager $em)
329+
public function __construct(EntityManager $entityManager)
325330
{
326-
$this->em = $em;
331+
$this->entityManager = $entityManager;
327332
}
328333

329334
public function buildForm(FormBuilderInterface $builder, array $options)
330335
{
331-
$transformer = new IssueToNumberTransformer($this->em);
336+
$transformer = new IssueToNumberTransformer($this->entityManager);
332337
$builder->addModelTransformer($transformer);
333338
}
334339

@@ -423,15 +428,15 @@ types of underlying data.
423428

424429
In any form, the three different types of data are:
425430

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()``,
428433
you're dealing with the "model" data.
429434

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
431436
the same as your "model" data (though not in our example). It's not commonly
432437
used directly.
433438

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
435440
themselves. It's also the format in which the user will submit the data. When
436441
you call ``Form::submit($data)``, the ``$data`` is in the "view" data format.
437442

@@ -463,3 +468,7 @@ The difference between the transformers is subtle and you should always think
463468
about what the "norm" data for a field should really be. For example, the
464469
"norm" data for a ``text`` field is a string, but is a ``DateTime`` object
465470
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

Comments
 (0)