@@ -16,10 +16,12 @@ to render the form, and then back into a ``DateTime`` object on submit.
1616 When a form field has the ``inherit_data `` option set, Data Transformers
1717 won't be applied to that field.
1818
19- Simple Example: Sanitizing HTML on User Input
20- ---------------------------------------------
19+ .. _simple-example-sanitizing-html-on-user-input :
2120
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::
2325
2426 // src/AppBundle/Form/TaskType.php
2527 namespace AppBundle\Form\Type;
@@ -32,7 +34,7 @@ Suppose you have a Task form with a description ``textarea`` type::
3234 {
3335 public function buildForm(FormBuilderInterface $builder, array $options)
3436 {
35- $builder->add('description ', 'textarea');
37+ $builder->add('tags ', 'text')
3638 }
3739
3840 public function configureOptions(OptionsResolver $resolver)
@@ -45,15 +47,10 @@ Suppose you have a Task form with a description ``textarea`` type::
4547 // ...
4648 }
4749
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 displayed to the user as a
51+ simple comma seperated string to make them easier to edit.
5252
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 ``
5754field. The easiest way to do this is with the :class: `Symfony\\ Component\\ Form\\ CallbackTransformer `
5855class::
5956
@@ -68,20 +65,17 @@ class::
6865 {
6966 public function buildForm(FormBuilderInterface $builder, array $options)
7067 {
71- $builder->add('description ', 'textarea ');
68+ $builder->add('tags ', 'text ');
7269
73- $builder->get('description ')
70+ $builder->get('tags ')
7471 ->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+ function ($tagsAsArray) {
73+ // transform the array to a string
74+ return implode(', ' , $tagsAsArray );
7875 },
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 ($tagsAsString) {
77+ // transform the string back to an array
78+ return explode(', ', $tagsAsString);
8579 }
8680 ))
8781 ;
@@ -90,10 +84,10 @@ class::
9084 // ...
9185 }
9286
93- The ``CallbackTransformer `` takes two callback functions as arguments. The first transforms
94- the original value into a format that'll be used to render the field. The second
95- does the reverse: it transforms the submitted value back into the format you'll use
96- in your code.
87+ The ``CallbackTransformer `` takes two callback functions as arguments. The
88+ first transforms the original value into a format that'll be used to render the
89+ field. The second does the reverse: it transforms the submitted value back into
90+ the format you'll use in your code.
9791
9892.. tip ::
9993
@@ -105,7 +99,8 @@ You can also add the transformer, right when adding the field by changing the fo
10599slightly::
106100
107101 $builder->add(
108- $builder->create('description', 'textarea')
102+ $builder
103+ ->create('tags', 'text')
109104 ->addModelTransformer(...)
110105 );
111106
0 commit comments