@@ -16,10 +16,12 @@ 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
20
- ---------------------------------------------
19
+ .. _simple-example-sanitizing-html-on-user-input :
21
20
22
- Suppose you have a Task form with a description ``textarea `` type::
21
+ Simple Example: Transforming String Tags from User Input to an Array
22
+ --------------------------------------------------------------------
23
+
24
+ Suppose you have a Task form with a tags ``text `` type::
23
25
24
26
// src/AppBundle/Form/TaskType.php
25
27
namespace AppBundle\Form\Type;
@@ -32,7 +34,7 @@ Suppose you have a Task form with a description ``textarea`` type::
32
34
{
33
35
public function buildForm(FormBuilderInterface $builder, array $options)
34
36
{
35
- $builder->add('description ', 'textarea');
37
+ $builder->add('tags ', 'text')
36
38
}
37
39
38
40
public function setDefaultOptions(OptionsResolverInterface $resolver)
@@ -45,15 +47,10 @@ Suppose you have a Task form with a description ``textarea`` type::
45
47
// ...
46
48
}
47
49
48
- But, there are two complications:
49
-
50
- #. Your users are allowed to use *some * HTML tags, but not others: you need a way
51
- to call :phpfunction: `strip_tags ` after the form is submitted;
50
+ Internally the ``tags `` are stored as an array, but they are displayed
51
+ to the user as a simple string, to make them easier to edit.
52
52
53
- #. To be friendly, you want to convert ``<br/> `` tags into line breaks (``\n ``) before
54
- rendering the field so the text is easier to edit.
55
-
56
- This is a *perfect * time to attach a custom data transformer to the ``description ``
53
+ This is a *perfect * time to attach a custom data transformer to the ``tags ``
57
54
field. The easiest way to do this is with the :class: `Symfony\\ Component\\ Form\\ CallbackTransformer `
58
55
class::
59
56
@@ -68,20 +65,17 @@ class::
68
65
{
69
66
public function buildForm(FormBuilderInterface $builder, array $options)
70
67
{
71
- $builder->add('description ', 'textarea ');
68
+ $builder->add('tags ', 'text ');
72
69
73
- $builder->get('description ')
70
+ $builder->get('tags ')
74
71
->addModelTransformer(new CallbackTransformer(
75
- // transform <br/> to \n so the textarea reads easier
76
- function ($originalDescription ) {
77
- return preg_replace('#<br\s*/?>#i', "\n" , $originalDescription );
72
+ // transform array to string so the input reads easier
73
+ function ($originalTags ) {
74
+ return implode(', ' , $originalTags );
78
75
},
79
- function ($submittedDescription) {
80
- // remove most HTML tags (but not br,p)
81
- $cleaned = strip_tags($submittedDescription, '<br><br/><p>');
82
-
83
- // transform any \n to real <br/>
84
- return str_replace("\n", '<br/>', $cleaned);
76
+ function ($submittedTags) {
77
+ // transform the string back to Array
78
+ return explode(', ', $submittedTags);
85
79
}
86
80
))
87
81
;
@@ -105,7 +99,7 @@ You can also add the transformer, right when adding the field by changing the fo
105
99
slightly::
106
100
107
101
$builder->add(
108
- $builder->create('description ', 'textarea ')
102
+ $builder->create('tags ', 'text ')
109
103
->addModelTransformer(...)
110
104
);
111
105
0 commit comments