-
-
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
Closed
mccullagh
wants to merge
7
commits into
symfony:master
from
mccullagh:feature/data_transforming_example
Closed
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
495ca23
[#6431] changing "Simple Example" to use implode/explode
mccullagh 45add8b
fix missing space after the comma
mccullagh 83f689c
changed explanation text
mccullagh 15f053b
Example from Labels to Tags
mccullagh 216b228
change simply text
mccullagh 310f6a4
add a label to be BC with the old headline
mccullagh 9dcc4b6
revert extra spaces
mccullagh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,24 +16,23 @@ 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 | ||
-------------------------------------------------------------------- | ||
|
||
Suppose you have a Task form with a description ``textarea`` type:: | ||
Suppose you have a Task form with a tags ``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; | ||
|
||
// ... | ||
class TaskType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder->add('description', TextareaType::class); | ||
$builder->add('tags', 'text') | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
|
@@ -46,15 +45,10 @@ 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 the ``tags`` are stored as an array, but they are displayed | ||
to the user as a simple string, to make them easier to edit. | ||
|
||
#. 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 ``tags`` | ||
field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\CallbackTransformer` | ||
class:: | ||
|
||
|
@@ -63,27 +57,23 @@ class:: | |
|
||
use Symfony\Component\Form\CallbackTransformer; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | ||
// ... | ||
|
||
class TaskType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder->add('description', TextareaType::class); | ||
$builder->add('tags', 'text'); | ||
|
||
$builder->get('description') | ||
$builder->get('tags') | ||
->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 ($originalTags) { | ||
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 should revert the extra spaces here |
||
return implode(', ', $originalTags); | ||
}, | ||
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 ($submittedTags) { | ||
// transform the string back to Array | ||
return explode(', ', $submittedTags); | ||
} | ||
)) | ||
; | ||
|
@@ -106,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\TextareaType; | ||
|
||
$builder->add( | ||
$builder->create('description', TextareaType::class) | ||
$builder->create('tags', 'text') | ||
->addModelTransformer(...) | ||
); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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: