@@ -22,6 +22,7 @@ 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
+ namespace AppBundle\Form\Type;
25
26
26
27
use Symfony\Component\Form\FormBuilderInterface;
27
28
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
@@ -31,14 +32,13 @@ Suppose you have a Task form with a description ``textarea`` type::
31
32
{
32
33
public function buildForm(FormBuilderInterface $builder, array $options)
33
34
{
34
- $builder
35
- ->add('description', 'textarea');
35
+ $builder->add('description', 'textarea');
36
36
}
37
-
37
+
38
38
public function setDefaultOptions(OptionsResolverInterface $resolver)
39
39
{
40
40
$resolver->setDefaults(array(
41
- 'data_class' => 'AppBundle\Entity\Task'
41
+ 'data_class' => 'AppBundle\Entity\Task',
42
42
));
43
43
}
44
44
@@ -58,6 +58,7 @@ field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\
58
58
class::
59
59
60
60
// src/AppBundle/Form/TaskType.php
61
+ namespace AppBundle\Form\Type;
61
62
62
63
use Symfony\Component\Form\CallbackTransformer;
63
64
use Symfony\Component\Form\FormBuilderInterface;
@@ -67,8 +68,7 @@ class::
67
68
{
68
69
public function buildForm(FormBuilderInterface $builder, array $options)
69
70
{
70
- $builder
71
- ->add('description', 'textarea');
71
+ $builder->add('description', 'textarea');
72
72
73
73
$builder->get('description')
74
74
->addModelTransformer(new CallbackTransformer(
@@ -83,7 +83,8 @@ class::
83
83
// transform any \n to real <br/>
84
84
return str_replace("\n", '<br/>', $cleaned);
85
85
}
86
- ));
86
+ ))
87
+ ;
87
88
}
88
89
89
90
// ...
@@ -120,6 +121,7 @@ issue number.
120
121
Start by setting up the text field like normal::
121
122
122
123
// src/AppBundle/Form/TaskType.php
124
+ namespace AppBundle\Form\Type;
123
125
124
126
// ...
125
127
class TaskType extends AbstractType
@@ -128,7 +130,8 @@ Start by setting up the text field like normal::
128
130
{
129
131
$builder
130
132
->add('description', 'textarea')
131
- ->add('issue', 'text');
133
+ ->add('issue', 'text')
134
+ ;
132
135
}
133
136
134
137
public function setDefaultOptions(OptionsResolverInterface $resolver)
@@ -247,6 +250,7 @@ No problem! Just add a ``__construct()`` function to ``TaskType`` and force this
247
250
to be passed in. Then, you can easily create and add the transformer::
248
251
249
252
// src/AppBundle/Form/TaskType.php
253
+ namespace AppBundle\Form\Type;
250
254
251
255
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
252
256
use Doctrine\Common\Persistence\EntityManager;
@@ -267,7 +271,7 @@ to be passed in. Then, you can easily create and add the transformer::
267
271
->add('description', 'textarea')
268
272
->add('issue', 'text', array(
269
273
// 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',
271
275
));
272
276
273
277
// ...
@@ -311,6 +315,8 @@ its error message can be controlled with the ``invalid_message`` field option.
311
315
$builder->add('issue', 'text')
312
316
->addModelTransformer($transformer);
313
317
318
+ .. _using-transformers-in-a-custom-field-type :
319
+
314
320
Creating a Reusable issue_selector Field
315
321
----------------------------------------
316
322
@@ -322,7 +328,6 @@ that does this automatically.
322
328
First, create the custom field type class::
323
329
324
330
// src/AppBundle/Form/IssueSelectorType.php
325
-
326
331
namespace AppBundle\Form;
327
332
328
333
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
@@ -385,24 +390,37 @@ it's recognized as a custom field type:
385
390
386
391
.. code-block :: xml
387
392
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 >
393
408
394
409
.. code-block :: php
395
410
411
+ // app/config/services.php
396
412
use Symfony\Component\DependencyInjection\Definition;
397
413
use Symfony\Component\DependencyInjection\Reference;
398
414
// ...
399
415
400
416
$container
401
417
->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
+ )
406
424
->addTag('form.type', array(
407
425
'alias' => 'issue_selector',
408
426
))
@@ -412,20 +430,26 @@ Now, whenever you need to use your special ``issue_selector`` field type,
412
430
it's quite easy::
413
431
414
432
// src/AppBundle/Form/TaskType.php
433
+ namespace AppBundle\Form\Type;
434
+
415
435
use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
436
+ // ...
416
437
417
438
class TaskType extends AbstractType
418
439
{
419
440
public function buildForm(FormBuilderInterface $builder, array $options)
420
441
{
421
442
$builder
422
443
->add('description', 'textarea')
423
- ->add('issue', 'issue_selector');
444
+ ->add('issue', 'issue_selector')
445
+ ;
424
446
}
425
447
426
448
// ...
427
449
}
428
450
451
+ .. _model-and-view-transformers :
452
+
429
453
About Model and View Transformers
430
454
---------------------------------
431
455
0 commit comments