@@ -16,24 +16,24 @@ to render the form, and then back into a ``DateTime`` object on submit.
16
16
When a form field has the ``inherit_data `` option set, Data Transformers
17
17
won't be applied to that field.
18
18
19
- Simple Example:Sanitizing HTML on User Input
19
+ Simple Example:transforming labels string from User Input to array
20
20
---------------------------------------------
21
21
22
- Suppose you have a Task form with adescription ``textarea `` type::
22
+ Suppose you have a Task form with alabels ``text `` type::
23
23
24
24
// src/AppBundle/Form/TaskType.php
25
25
namespace AppBundle\Form\Type;
26
26
27
27
use Symfony\Component\Form\FormBuilderInterface;
28
28
use Symfony\Component\OptionsResolver\OptionsResolver;
29
- use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
29
+ use Symfony\Component\Form\Extension\Core\Type\TextType ;
30
30
31
31
// ...
32
32
class TaskType extends AbstractType
33
33
{
34
34
public function buildForm(FormBuilderInterface $builder, array $options)
35
35
{
36
- $builder->add('description ',TextareaType ::class);
36
+ $builder->add('labels ',TextType ::class);
37
37
}
38
38
39
39
public function configureOptions(OptionsResolver $resolver)
@@ -46,15 +46,9 @@ Suppose you have a Task form with a description ``textarea`` type::
46
46
// ...
47
47
}
48
48
49
- But, there are two complications:
50
-
51
- #. Your users are allowed to use *some * HTML tags, but not others: you need a way
52
- to call:phpfunction: `strip_tags ` after the form is submitted;
49
+ Internally we want to handle the ``labels `` as array, but to have the form simple we wanna allow the User to edit them as a string.
53
50
54
- #. To be friendly, you want to convert ``<br/> `` tags into line breaks (``\n ``) before
55
- rendering the field so the text is easier to edit.
56
-
57
- This is a *perfect * time to attach a custom data transformer to the ``description ``
51
+ This is a *perfect * time to attach a custom data transformer to the ``labels ``
58
52
field. The easiest way to do this is with the:class: `Symfony\\ Component\\ Form\\ CallbackTransformer `
59
53
class::
60
54
@@ -63,27 +57,24 @@ class::
63
57
64
58
use Symfony\Component\Form\CallbackTransformer;
65
59
use Symfony\Component\Form\FormBuilderInterface;
66
- use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
60
+ use Symfony\Component\Form\Extension\Core\Type\TextType ;
67
61
// ...
68
62
69
63
class TaskType extends AbstractType
70
64
{
71
65
public function buildForm(FormBuilderInterface $builder, array $options)
72
66
{
73
- $builder->add('description ',TextareaType ::class);
67
+ $builder->add('labels ',TextType ::class);
74
68
75
- $builder->get('description ')
69
+ $builder->get('labels ')
76
70
->addModelTransformer(new CallbackTransformer(
77
- // transform<br/> to\n so thetextarea reads easier
78
- function ($originalDescription ) {
79
- returnpreg_replace('#<br\s*/?>#i ',"\n", $originalDescription );
71
+ // transformarray tostring so theinput reads easier
72
+ function ($originalLabels ) {
73
+ returnimplode(', ',$originalLabels );
80
74
},
81
- function ($submittedDescription) {
82
- // remove most HTML tags (but not br,p)
83
- $cleaned = strip_tags($submittedDescription, '<br><br/><p>');
84
-
85
- // transform any \n to real <br/>
86
- return str_replace("\n", '<br/>', $cleaned);
75
+ function ($submittedLabels) {
76
+ // transform the string back to Array
77
+ return explode(',', $submittedLabels);
87
78
}
88
79
))
89
80
;
@@ -106,10 +97,10 @@ in your code.
106
97
You can also add the transformer, right when adding the field by changing the format
107
98
slightly::
108
99
109
- use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
100
+ use Symfony\Component\Form\Extension\Core\Type\TextType ;
110
101
111
102
$builder->add(
112
- $builder->create('description',TextareaType ::class)
103
+ $builder->create('description',TextType ::class)
113
104
->addModelTransformer(...)
114
105
);
115
106