@@ -16,24 +16,26 @@ 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;
2628
2729 use Symfony\Component\Form\FormBuilderInterface;
2830 use Symfony\Component\OptionsResolver\OptionsResolver;
29- use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
31+ use Symfony\Component\Form\Extension\Core\Type\TextType ;
3032
3133 // ...
3234 class TaskType extends AbstractType
3335 {
3436 public function buildForm(FormBuilderInterface $builder, array $options)
3537 {
36- $builder->add('description ', TextareaType ::class);
38+ $builder->add('tags ', TextType ::class)
3739 }
3840
3941 public function configureOptions(OptionsResolver $resolver)
@@ -46,15 +48,10 @@ Suppose you have a Task form with a description ``textarea`` type::
4648 // ...
4749 }
4850
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;
53-
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.
51+ Internally the ``tags `` are stored as an array, but displayed to the user as a
52+ simple comma seperated string to make them easier to edit.
5653
57- This is a *perfect * time to attach a custom data transformer to the ``description ``
54+ This is a *perfect * time to attach a custom data transformer to the ``tags ``
5855field. The easiest way to do this is with the :class: `Symfony\\ Component\\ Form\\ CallbackTransformer `
5956class::
6057
@@ -63,27 +60,24 @@ class::
6360
6461 use Symfony\Component\Form\CallbackTransformer;
6562 use Symfony\Component\Form\FormBuilderInterface;
66- use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
63+ use Symfony\Component\Form\Extension\Core\Type\TextType ;
6764 // ...
6865
6966 class TaskType extends AbstractType
7067 {
7168 public function buildForm(FormBuilderInterface $builder, array $options)
7269 {
73- $builder->add('description ', TextareaType ::class);
70+ $builder->add('tags ', TextType ::class);
7471
75- $builder->get('description ')
72+ $builder->get('tags ')
7673 ->addModelTransformer(new CallbackTransformer(
77- // transform <br/> to \n so the textarea reads easier
78- function ($originalDescription) {
79- return preg_replace('#<br\s*/?>#i', "\n" , $originalDescription );
74+ function ($tagsAsArray) {
75+ // transform the array to a string
76+ return implode(', ' , $tagsAsArray );
8077 },
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);
78+ function ($tagsAsString) {
79+ // transform the string back to an array
80+ return explode(', ', $tagsAsString);
8781 }
8882 ))
8983 ;
@@ -92,10 +86,10 @@ class::
9286 // ...
9387 }
9488
95- The ``CallbackTransformer `` takes two callback functions as arguments. The first transforms
96- the original value into a format that'll be used to render the field. The second
97- does the reverse: it transforms the submitted value back into the format you'll use
98- in your code.
89+ The ``CallbackTransformer `` takes two callback functions as arguments. The
90+ first transforms the original value into a format that'll be used to render the
91+ field. The second does the reverse: it transforms the submitted value back into
92+ the format you'll use in your code.
9993
10094.. tip ::
10195
@@ -106,10 +100,11 @@ in your code.
106100You can also add the transformer, right when adding the field by changing the format
107101slightly::
108102
109- use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
103+ use Symfony\Component\Form\Extension\Core\Type\TextType ;
110104
111105 $builder->add(
112- $builder->create('description', TextareaType::class)
106+ $builder
107+ ->create('tags', TextType::class)
113108 ->addModelTransformer(...)
114109 );
115110
0 commit comments