Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb1ec264

Browse files
mccullaghwouterj
authored andcommitted
[#6431] changing "Simple Example" to use implode/explode
1 parented51bdc commitb1ec264

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

‎cookbook/form/data_transformers.rst

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ 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;
@@ -32,7 +34,7 @@ Suppose you have a Task form with a description ``textarea`` type::
3234
{
3335
public function buildForm(FormBuilderInterface $builder, array $options)
3436
{
35-
$builder->add('description', 'textarea');
37+
$builder->add('tags', 'text')
3638
}
3739

3840
public function setDefaultOptions(OptionsResolverInterface $resolver)
@@ -45,15 +47,10 @@ Suppose you have a Task form with a description ``textarea`` type::
4547
// ...
4648
}
4749

48-
But, there are two complications:
49-
50-
#. Your users are allowed to use *some* HTML tags, but not others: you need a way
51-
to call:phpfunction:`strip_tags` after the form is submitted;
50+
Internally the ``tags`` are stored as an array, but they are displayed
51+
to the user as a simple string, to make them easier to edit.
5252

53-
#. To be friendly, you want to convert ``<br/>`` tags into line breaks (``\n``) before
54-
rendering the field so the text is easier to edit.
55-
56-
This is a *perfect* time to attach a custom data transformer to the ``description``
53+
This is a *perfect* time to attach a custom data transformer to the ``tags``
5754
field. The easiest way to do this is with the:class:`Symfony\\Component\\Form\\CallbackTransformer`
5855
class::
5956

@@ -68,20 +65,17 @@ class::
6865
{
6966
public function buildForm(FormBuilderInterface $builder, array $options)
7067
{
71-
$builder->add('description', 'textarea');
68+
$builder->add('tags', 'text');
7269

73-
$builder->get('description')
70+
$builder->get('tags')
7471
->addModelTransformer(new CallbackTransformer(
75-
// transform<br/> to\n so thetextarea reads easier
76-
function ($originalDescription) {
77-
returnpreg_replace('#<br\s*/?>#i', "\n", $originalDescription);
72+
// transformarray tostring so theinput reads easier
73+
function ($originalTags) {
74+
returnimplode(', ', $originalTags);
7875
},
79-
function ($submittedDescription) {
80-
// remove most HTML tags (but not br,p)
81-
$cleaned = strip_tags($submittedDescription, '<br><br/><p>');
82-
83-
// transform any \n to real <br/>
84-
return str_replace("\n", '<br/>', $cleaned);
76+
function ($submittedTags) {
77+
// transform the string back to Array
78+
return explode(', ', $submittedTags);
8579
}
8680
))
8781
;
@@ -105,7 +99,7 @@ You can also add the transformer, right when adding the field by changing the fo
10599
slightly::
106100

107101
$builder->add(
108-
$builder->create('description', 'textarea')
102+
$builder->create('tags', 'text')
109103
->addModelTransformer(...)
110104
);
111105

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp