Skip to content

Commit

Permalink
minor #5505 [Cookbook][Form] some tweaks to the data transformers cha…
Browse files Browse the repository at this point in the history
…pter (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Cookbook][Form] some tweaks to the data transformers chapter

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets |

* updates some code blocks
* adds labels to keep BC for old headlines

Commits
-------

33e5613 some tweaks to the data transformers chapter
  • Loading branch information
weaverryan committed Jul 12, 2015
2 parents f9ef3ee + 33e5613 commit 91d9ab1
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions cookbook/form/data_transformers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Simple Example: Sanitizing HTML on User Input
Suppose you have a Task form with a description ``textarea`` type::

// src/AppBundle/Form/TaskType.php
namespace AppBundle\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand All @@ -31,14 +32,13 @@ Suppose you have a Task form with a description ``textarea`` type::
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description', 'textarea');
$builder->add('description', 'textarea');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Task'
'data_class' => 'AppBundle\Entity\Task',
));
}

Expand All @@ -58,6 +58,7 @@ field. The easiest way to do this is with the :class:`Symfony\\Component\\Form\\
class::

// src/AppBundle/Form/TaskType.php
namespace AppBundle\Form\Type;

use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -67,8 +68,7 @@ class::
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description', 'textarea');
$builder->add('description', 'textarea');

$builder->get('description')
->addModelTransformer(new CallbackTransformer(
Expand All @@ -83,7 +83,8 @@ class::
// transform any \n to real <br/>
return str_replace("\n", '<br/>', $cleaned);
}
));
))
;
}

// ...
Expand Down Expand Up @@ -120,6 +121,7 @@ issue number.
Start by setting up the text field like normal::

// src/AppBundle/Form/TaskType.php
namespace AppBundle\Form\Type;

// ...
class TaskType extends AbstractType
Expand All @@ -128,7 +130,8 @@ Start by setting up the text field like normal::
{
$builder
->add('description', 'textarea')
->add('issue', 'text');
->add('issue', 'text')
;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
Expand Down Expand Up @@ -247,6 +250,7 @@ No problem! Just add a ``__construct()`` function to ``TaskType`` and force this
to be passed in. Then, you can easily create and add the transformer::

// src/AppBundle/Form/TaskType.php
namespace AppBundle\Form\Type;

use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
use Doctrine\Common\Persistence\EntityManager;
Expand All @@ -267,7 +271,7 @@ to be passed in. Then, you can easily create and add the transformer::
->add('description', 'textarea')
->add('issue', 'text', array(
// validation message if the data transformer fails
'invalid_message' => 'That is not a valid issue number'
'invalid_message' => 'That is not a valid issue number',
));

// ...
Expand Down Expand Up @@ -311,6 +315,8 @@ its error message can be controlled with the ``invalid_message`` field option.
$builder->add('issue', 'text')
->addModelTransformer($transformer);

.. _using-transformers-in-a-custom-field-type:

Creating a Reusable issue_selector Field
----------------------------------------

Expand All @@ -322,7 +328,6 @@ that does this automatically.
First, create the custom field type class::

// src/AppBundle/Form/IssueSelectorType.php

namespace AppBundle\Form;

use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
Expand Down Expand Up @@ -385,24 +390,37 @@ it's recognized as a custom field type:
.. code-block:: xml
<service id="app.type.issue_selector"
class="AppBundle\Form\IssueSelectorType">
<argument type="service" id="doctrine.orm.entity_manager"/>
<tag name="form.type" alias="issue_selector" />
</service>
<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="app.type.issue_selector"
class="AppBundle\Form\IssueSelectorType">
<argument type="service" id="doctrine.orm.entity_manager"/>
<tag name="form.type" alias="issue_selector" />
</service>
</services>
</container>
.. code-block:: php
// app/config/services.php
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
// ...
$container
->setDefinition('app.type.issue_selector', new Definition(
'AppBundle\Form\IssueSelectorType'
), array(
new Reference('doctrine.orm.entity_manager'),
))
'AppBundle\Form\IssueSelectorType'
),
array(
new Reference('doctrine.orm.entity_manager'),
)
)
->addTag('form.type', array(
'alias' => 'issue_selector',
))
Expand All @@ -412,20 +430,26 @@ Now, whenever you need to use your special ``issue_selector`` field type,
it's quite easy::

// src/AppBundle/Form/TaskType.php
namespace AppBundle\Form\Type;

use AppBundle\Form\DataTransformer\IssueToNumberTransformer;
// ...

class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description', 'textarea')
->add('issue', 'issue_selector');
->add('issue', 'issue_selector')
;
}

// ...
}

.. _model-and-view-transformers:

About Model and View Transformers
---------------------------------

Expand Down

0 comments on commit 91d9ab1

Please sign in to comment.