-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#6431] changing "Simple Example" to use implode/explode #6581
Changes from 1 commit
495ca23
45add8b
83f689c
15f053b
216b228
310f6a4
9dcc4b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
------------------------------------------------------------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a label to be BC with the old headline? This should look like this: .. _simple-example-sanitizing-html-on-user-input: |
||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this go on 2.3? |
||
} | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use "we" in the docs. What if we reword this as:
|
||
|
||
#. To be friendly, you want to convert ``<br/>`` 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 <br/> to \n so the textarea reads easier | ||
function ($originalDescription) { | ||
return preg_replace('#<br\s*/?>#i', "\n", $originalDescription); | ||
// transform array to string so the input reads easier | ||
function ($originalLabels) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we rename these variables:
This is just a proposal. Please don't make any change until our doc managers approve/reject this idea. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like your proposal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. original/submitted keeps an "event" context, and I like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. original/submitted helps to identify the source of the Callbacks. |
||
return implode(',', $originalLabels); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing space after the comma |
||
}, | ||
function ($submittedDescription) { | ||
// remove most HTML tags (but not br,p) | ||
$cleaned = strip_tags($submittedDescription, '<br><br/><p>'); | ||
|
||
// transform any \n to real <br/> | ||
return str_replace("\n", '<br/>', $cleaned); | ||
function ($submittedLabels) { | ||
// transform the string back to Array | ||
return explode(',', $submittedLabels); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing a space after the comma |
||
} | ||
)) | ||
; | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since text type is the default, I would suggest to remove the second argument everywhere to easy the merge in all branches. Why not also add a tip to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've pushed 133a77c, this should really go on 2.3 imo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can still pass |
||
->addModelTransformer(...) | ||
); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
Transforming Labels String from User Input to Array