Skip to content

Commit 33e5613

Browse files
committed
some tweaks to the data transformers chapter
* updates some code blocks * adds labels to keep BC for old headlines
1 parent 4abc22c commit 33e5613

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

cookbook/form/data_transformers.rst

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ 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+
namespace AppBundle\Form\Type;
2526

2627
use Symfony\Component\Form\FormBuilderInterface;
2728
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
@@ -31,14 +32,13 @@ Suppose you have a Task form with a description ``textarea`` type::
3132
{
3233
public function buildForm(FormBuilderInterface $builder, array $options)
3334
{
34-
$builder
35-
->add('description', 'textarea');
35+
$builder->add('description', 'textarea');
3636
}
37-
37+
3838
public function setDefaultOptions(OptionsResolverInterface $resolver)
3939
{
4040
$resolver->setDefaults(array(
41-
'data_class' => 'AppBundle\Entity\Task'
41+
'data_class' => 'AppBundle\Entity\Task',
4242
));
4343
}
4444

@@ -58,6 +58,7 @@ field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\
5858
class::
5959

6060
// src/AppBundle/Form/TaskType.php
61+
namespace AppBundle\Form\Type;
6162

6263
use Symfony\Component\Form\CallbackTransformer;
6364
use Symfony\Component\Form\FormBuilderInterface;
@@ -67,8 +68,7 @@ class::
6768
{
6869
public function buildForm(FormBuilderInterface $builder, array $options)
6970
{
70-
$builder
71-
->add('description', 'textarea');
71+
$builder->add('description', 'textarea');
7272

7373
$builder->get('description')
7474
->addModelTransformer(new CallbackTransformer(
@@ -83,7 +83,8 @@ class::
8383
// transform any \n to real <br/>
8484
return str_replace("\n", '<br/>', $cleaned);
8585
}
86-
));
86+
))
87+
;
8788
}
8889

8990
// ...
@@ -120,6 +121,7 @@ issue number.
120121
Start by setting up the text field like normal::
121122

122123
// src/AppBundle/Form/TaskType.php
124+
namespace AppBundle\Form\Type;
123125

124126
// ...
125127
class TaskType extends AbstractType
@@ -128,7 +130,8 @@ Start by setting up the text field like normal::
128130
{
129131
$builder
130132
->add('description', 'textarea')
131-
->add('issue', 'text');
133+
->add('issue', 'text')
134+
;
132135
}
133136

134137
public function setDefaultOptions(OptionsResolverInterface $resolver)
@@ -247,6 +250,7 @@ No problem! Just add a ``__construct()`` function to ``TaskType`` and force this
247250
to be passed in. Then, you can easily create and add the transformer::
248251

249252
// src/AppBundle/Form/TaskType.php
253+
namespace AppBundle\Form\Type;
250254

251255
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
252256
use Doctrine\Common\Persistence\EntityManager;
@@ -267,7 +271,7 @@ to be passed in. Then, you can easily create and add the transformer::
267271
->add('description', 'textarea')
268272
->add('issue', 'text', array(
269273
// validation message if the data transformer fails
270-
'invalid_message' => 'That is not a valid issue number'
274+
'invalid_message' => 'That is not a valid issue number',
271275
));
272276

273277
// ...
@@ -311,6 +315,8 @@ its error message can be controlled with the ``invalid_message`` field option.
311315
$builder->add('issue', 'text')
312316
->addModelTransformer($transformer);
313317

318+
.. _using-transformers-in-a-custom-field-type:
319+
314320
Creating a Reusable issue_selector Field
315321
----------------------------------------
316322

@@ -322,7 +328,6 @@ that does this automatically.
322328
First, create the custom field type class::
323329

324330
// src/AppBundle/Form/IssueSelectorType.php
325-
326331
namespace AppBundle\Form;
327332

328333
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
@@ -385,24 +390,37 @@ it's recognized as a custom field type:
385390
386391
.. code-block:: xml
387392
388-
<service id="app.type.issue_selector"
389-
class="AppBundle\Form\IssueSelectorType">
390-
<argument type="service" id="doctrine.orm.entity_manager"/>
391-
<tag name="form.type" alias="issue_selector" />
392-
</service>
393+
<!-- app/config/services.xml -->
394+
<?xml version="1.0" encoding="UTF-8" ?>
395+
<container xmlns="http://symfony.com/schema/dic/services"
396+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
397+
xsi:schemaLocation="http://symfony.com/schema/dic/services
398+
http://symfony.com/schema/dic/services/services-1.0.xsd">
399+
400+
<services>
401+
<service id="app.type.issue_selector"
402+
class="AppBundle\Form\IssueSelectorType">
403+
<argument type="service" id="doctrine.orm.entity_manager"/>
404+
<tag name="form.type" alias="issue_selector" />
405+
</service>
406+
</services>
407+
</container>
393408
394409
.. code-block:: php
395410
411+
// app/config/services.php
396412
use Symfony\Component\DependencyInjection\Definition;
397413
use Symfony\Component\DependencyInjection\Reference;
398414
// ...
399415
400416
$container
401417
->setDefinition('app.type.issue_selector', new Definition(
402-
'AppBundle\Form\IssueSelectorType'
403-
), array(
404-
new Reference('doctrine.orm.entity_manager'),
405-
))
418+
'AppBundle\Form\IssueSelectorType'
419+
),
420+
array(
421+
new Reference('doctrine.orm.entity_manager'),
422+
)
423+
)
406424
->addTag('form.type', array(
407425
'alias' => 'issue_selector',
408426
))
@@ -412,20 +430,26 @@ Now, whenever you need to use your special ``issue_selector`` field type,
412430
it's quite easy::
413431

414432
// src/AppBundle/Form/TaskType.php
433+
namespace AppBundle\Form\Type;
434+
415435
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
436+
// ...
416437

417438
class TaskType extends AbstractType
418439
{
419440
public function buildForm(FormBuilderInterface $builder, array $options)
420441
{
421442
$builder
422443
->add('description', 'textarea')
423-
->add('issue', 'issue_selector');
444+
->add('issue', 'issue_selector')
445+
;
424446
}
425447

426448
// ...
427449
}
428450

451+
.. _model-and-view-transformers:
452+
429453
About Model and View Transformers
430454
---------------------------------
431455

0 commit comments

Comments
 (0)