From 495ca23ecbbb2561d9371630061090807ae0ae7e Mon Sep 17 00:00:00 2001 From: mccullagh Date: Sat, 21 May 2016 12:05:19 +0200 Subject: [PATCH 1/7] [#6431] changing "Simple Example" to use implode/explode --- cookbook/form/data_transformers.rst | 45 ++++++++++++----------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index 680758ddd6d..4f9f49835ad 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -16,24 +16,24 @@ to render the form, and then back into a ``DateTime`` object on submit. When a form field has the ``inherit_data`` option set, Data Transformers won't be applied to that field. -Simple Example: Sanitizing HTML on User Input ---------------------------------------------- +Simple Example: transforming labels string from User Input to array +------------------------------------------------------------------- -Suppose you have a Task form with a description ``textarea`` type:: +Suppose you have a Task form with a labels ``text`` type:: // src/AppBundle/Form/TaskType.php namespace AppBundle\Form\Type; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; - use Symfony\Component\Form\Extension\Core\Type\TextareaType; + use Symfony\Component\Form\Extension\Core\Type\TextType; // ... class TaskType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('description', TextareaType::class); + $builder->add('labels', TextType::class); } public function configureOptions(OptionsResolver $resolver) @@ -46,15 +46,9 @@ Suppose you have a Task form with a description ``textarea`` type:: // ... } -But, there are two complications: - -#. Your users are allowed to use *some* HTML tags, but not others: you need a way - to call :phpfunction:`strip_tags` after the form is submitted; +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. -#. To be friendly, you want to convert ``
`` tags into line breaks (``\n``) before - rendering the field so the text is easier to edit. - -This is a *perfect* time to attach a custom data transformer to the ``description`` +This is a *perfect* time to attach a custom data transformer to the ``labels`` field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\CallbackTransformer` class:: @@ -63,27 +57,24 @@ class:: use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\FormBuilderInterface; - use Symfony\Component\Form\Extension\Core\Type\TextareaType; + use Symfony\Component\Form\Extension\Core\Type\TextType; // ... class TaskType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('description', TextareaType::class); + $builder->add('labels', TextType::class); - $builder->get('description') + $builder->get('labels') ->addModelTransformer(new CallbackTransformer( - // transform
to \n so the textarea reads easier - function ($originalDescription) { - return preg_replace('##i', "\n", $originalDescription); + // transform array to string so the input reads easier + function ($originalLabels) { + return implode(',', $originalLabels); }, - function ($submittedDescription) { - // remove most HTML tags (but not br,p) - $cleaned = strip_tags($submittedDescription, '

'); - - // transform any \n to real
- return str_replace("\n", '
', $cleaned); + function ($submittedLabels) { + // transform the string back to Array + return explode(',', $submittedLabels); } )) ; @@ -106,10 +97,10 @@ in your code. You can also add the transformer, right when adding the field by changing the format slightly:: - use Symfony\Component\Form\Extension\Core\Type\TextareaType; + use Symfony\Component\Form\Extension\Core\Type\TextType; $builder->add( - $builder->create('description', TextareaType::class) + $builder->create('description', TextType::class) ->addModelTransformer(...) ); From 45add8bbfd3f1c165200ffbec04f546aff67c29e Mon Sep 17 00:00:00 2001 From: mccullagh Date: Sat, 21 May 2016 12:40:33 +0200 Subject: [PATCH 2/7] fix missing space after the comma --- cookbook/form/data_transformers.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index 4f9f49835ad..c2c707b5d39 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -70,11 +70,11 @@ class:: ->addModelTransformer(new CallbackTransformer( // transform array to string so the input reads easier function ($originalLabels) { - return implode(',', $originalLabels); + return implode(', ', $originalLabels); }, function ($submittedLabels) { // transform the string back to Array - return explode(',', $submittedLabels); + return explode(', ', $submittedLabels); } )) ; From 83f689c4fb4bcf19d57462a172e8cee8c3c04466 Mon Sep 17 00:00:00 2001 From: mccullagh Date: Sat, 21 May 2016 12:47:13 +0200 Subject: [PATCH 3/7] changed explanation text --- cookbook/form/data_transformers.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index c2c707b5d39..0bdd91833dc 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -46,7 +46,8 @@ Suppose you have a Task form with a labels ``text`` type:: // ... } -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. +Internally the ``labels`` are stored as an array, but they are displayed +to the user as a simple string, to make them easier to edit. This is a *perfect* time to attach a custom data transformer to the ``labels`` field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\CallbackTransformer` From 15f053baa1e27c01bbb0bf6791b9b544c6f1af98 Mon Sep 17 00:00:00 2001 From: mccullagh Date: Sat, 21 May 2016 13:37:04 +0200 Subject: [PATCH 4/7] Example from Labels to Tags --- cookbook/form/data_transformers.rst | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index 0bdd91833dc..24c6af54806 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -16,10 +16,10 @@ to render the form, and then back into a ``DateTime`` object on submit. When a form field has the ``inherit_data`` option set, Data Transformers won't be applied to that field. -Simple Example: transforming labels string from User Input to array -------------------------------------------------------------------- +Simple Example: Transforming tags string from User Input to array +----------------------------------------------------------------- -Suppose you have a Task form with a labels ``text`` type:: +Suppose you have a Task form with a tags ``text`` type:: // src/AppBundle/Form/TaskType.php namespace AppBundle\Form\Type; @@ -33,7 +33,7 @@ Suppose you have a Task form with a labels ``text`` type:: { public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('labels', TextType::class); + $builder->add('tags', TextType::class); } public function configureOptions(OptionsResolver $resolver) @@ -46,10 +46,10 @@ Suppose you have a Task form with a labels ``text`` type:: // ... } -Internally the ``labels`` are stored as an array, but they are displayed +Internally the ``tags`` are stored as an array, but they are displayed to the user as a simple string, to make them easier to edit. -This is a *perfect* time to attach a custom data transformer to the ``labels`` +This is a *perfect* time to attach a custom data transformer to the ``tags`` field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\CallbackTransformer` class:: @@ -65,17 +65,17 @@ class:: { public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('labels', TextType::class); + $builder->add('tags', TextType::class); - $builder->get('labels') + $builder->get('tags') ->addModelTransformer(new CallbackTransformer( // transform array to string so the input reads easier - function ($originalLabels) { - return implode(', ', $originalLabels); + function ($originalTags) { + return implode(', ', $originalTags); }, - function ($submittedLabels) { + function ($submittedTags) { // transform the string back to Array - return explode(', ', $submittedLabels); + return explode(', ', $submittedTags); } )) ; @@ -101,7 +101,7 @@ slightly:: use Symfony\Component\Form\Extension\Core\Type\TextType; $builder->add( - $builder->create('description', TextType::class) + $builder->create('tags', TextType::class) ->addModelTransformer(...) ); From 216b22876f723d905f78578520f7c236673ad248 Mon Sep 17 00:00:00 2001 From: mccullagh Date: Sat, 21 May 2016 14:00:33 +0200 Subject: [PATCH 5/7] change simply text --- cookbook/form/data_transformers.rst | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index 24c6af54806..a6591478a72 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -16,8 +16,8 @@ to render the form, and then back into a ``DateTime`` object on submit. When a form field has the ``inherit_data`` option set, Data Transformers won't be applied to that field. -Simple Example: Transforming tags string from User Input to array ------------------------------------------------------------------ +Simple Example: Transforming String Tags from User Input to an Array +-------------------------------------------------------------------- Suppose you have a Task form with a tags ``text`` type:: @@ -26,14 +26,13 @@ Suppose you have a Task form with a tags ``text`` type:: use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; - use Symfony\Component\Form\Extension\Core\Type\TextType; // ... class TaskType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('tags', TextType::class); + $builder->add('tags', 'text') } public function configureOptions(OptionsResolver $resolver) @@ -58,19 +57,18 @@ class:: use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\FormBuilderInterface; - use Symfony\Component\Form\Extension\Core\Type\TextType; // ... class TaskType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('tags', TextType::class); + $builder->add('tags', 'text'); $builder->get('tags') ->addModelTransformer(new CallbackTransformer( // transform array to string so the input reads easier - function ($originalTags) { + function ($originalTags) { return implode(', ', $originalTags); }, function ($submittedTags) { @@ -98,10 +96,8 @@ in your code. You can also add the transformer, right when adding the field by changing the format slightly:: - use Symfony\Component\Form\Extension\Core\Type\TextType; - $builder->add( - $builder->create('tags', TextType::class) + $builder->create('tags', 'text') ->addModelTransformer(...) ); From 310f6a4b24a127015c95a3d6cbfb2223b755b3c3 Mon Sep 17 00:00:00 2001 From: mccullagh Date: Sat, 21 May 2016 14:22:36 +0200 Subject: [PATCH 6/7] add a label to be BC with the old headline --- cookbook/form/data_transformers.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index a6591478a72..42db2f330a1 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -16,6 +16,8 @@ to render the form, and then back into a ``DateTime`` object on submit. When a form field has the ``inherit_data`` option set, Data Transformers won't be applied to that field. +.. _simple-example-sanitizing-html-on-user-input: + Simple Example: Transforming String Tags from User Input to an Array -------------------------------------------------------------------- From 9dcc4b6324e517ad1518d88cd66cadd276056ba0 Mon Sep 17 00:00:00 2001 From: mccullagh Date: Sat, 21 May 2016 15:19:08 +0200 Subject: [PATCH 7/7] revert extra spaces --- cookbook/form/data_transformers.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index 42db2f330a1..8a85a6d0985 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -70,7 +70,7 @@ class:: $builder->get('tags') ->addModelTransformer(new CallbackTransformer( // transform array to string so the input reads easier - function ($originalTags) { + function ($originalTags) { return implode(', ', $originalTags); }, function ($submittedTags) {