Skip to content

Commit 495ca23

Browse files
committed
[#6431] changing "Simple Example" to use implode/explode
1 parent 1f1306a commit 495ca23

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

cookbook/form/data_transformers.rst

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ 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: transforming labels string from User Input to array
20+
-------------------------------------------------------------------
2121

22-
Suppose you have a Task form with a description ``textarea`` type::
22+
Suppose you have a Task form with a labels ``text`` type::
2323

2424
// src/AppBundle/Form/TaskType.php
2525
namespace AppBundle\Form\Type;
2626

2727
use Symfony\Component\Form\FormBuilderInterface;
2828
use Symfony\Component\OptionsResolver\OptionsResolver;
29-
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
29+
use Symfony\Component\Form\Extension\Core\Type\TextType;
3030

3131
// ...
3232
class TaskType extends AbstractType
3333
{
3434
public function buildForm(FormBuilderInterface $builder, array $options)
3535
{
36-
$builder->add('description', TextareaType::class);
36+
$builder->add('labels', TextType::class);
3737
}
3838

3939
public function configureOptions(OptionsResolver $resolver)
@@ -46,15 +46,9 @@ Suppose you have a Task form with a description ``textarea`` type::
4646
// ...
4747
}
4848

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.
5350

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``
5852
field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\CallbackTransformer`
5953
class::
6054

@@ -63,27 +57,24 @@ class::
6357

6458
use Symfony\Component\Form\CallbackTransformer;
6559
use Symfony\Component\Form\FormBuilderInterface;
66-
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
60+
use Symfony\Component\Form\Extension\Core\Type\TextType;
6761
// ...
6862

6963
class TaskType extends AbstractType
7064
{
7165
public function buildForm(FormBuilderInterface $builder, array $options)
7266
{
73-
$builder->add('description', TextareaType::class);
67+
$builder->add('labels', TextType::class);
7468

75-
$builder->get('description')
69+
$builder->get('labels')
7670
->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);
71+
// transform array to string so the input reads easier
72+
function ($originalLabels) {
73+
return implode(',', $originalLabels);
8074
},
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);
8778
}
8879
))
8980
;
@@ -106,10 +97,10 @@ in your code.
10697
You can also add the transformer, right when adding the field by changing the format
10798
slightly::
10899

109-
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
100+
use Symfony\Component\Form\Extension\Core\Type\TextType;
110101

111102
$builder->add(
112-
$builder->create('description', TextareaType::class)
103+
$builder->create('description', TextType::class)
113104
->addModelTransformer(...)
114105
);
115106

0 commit comments

Comments
 (0)