[Form] let `TextType` implements `DataTransformerInterface`

closes #5906.

The submitted data should always be
transformed back to the model as a string
as NULL in this case could stand for "unset
this value" whereas a string property of a
class could rely on the string type.

Furthermore, this prevents potential issues
with PHP 7 which allows type hinting of
strings in functions.
This commit is contained in:
Jules Pietri 2016-03-30 08:05:41 +02:00
parent 29469323eb
commit 7e97fbb6e4
4 changed files with 73 additions and 1 deletions

View File

@ -18,6 +18,8 @@ Form
* Support for data objects that implements both `Traversable` and `ArrayAccess`
in `ResizeFormListener::preSubmit` method has been deprecated and will be
removed in Symfony 4.0.
* `TextType` now implements `DataTransformerInterface` and will always return
an empty string when `empty_data` option is explicitly assigned to it.
FrameworkBundle
---------------

View File

@ -7,6 +7,7 @@ CHANGELOG
* deprecated the "choices_as_values" option of ChoiceType
* deprecated support for data objects that implements both `Traversable` and
`ArrayAccess` in `ResizeFormListener::preSubmit` method
* implemented `DataTransformerInterface` in `TextType`
3.0.0
-----

View File

@ -12,10 +12,24 @@
namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TextType extends AbstractType
class TextType extends AbstractType implements DataTransformerInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// When empty_data is explicitly set to an empty string,
// a string should always be returned when NULL is submitted
// This gives more control and thus helps preventing some issues
// with PHP 7 which allows type hinting strings in functions
// See https://github.com/symfony/symfony/issues/5906#issuecomment-203189375
if ('' === $options['empty_data']) {
$builder->addViewTransformer($this);
}
}
/**
* {@inheritdoc}
*/
@ -33,4 +47,22 @@ class TextType extends AbstractType
{
return 'text';
}
/**
* {@inheritdoc}
*/
public function transform($data)
{
// Model data should not be transformed
return $data;
}
/**
* {@inheritdoc}
*.
*/
public function reverseTransform($data)
{
return null === $data ? '' : $data;
}
}

View File

@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
use Symfony\Component\Form\Test\TypeTestCase as TestCase;
class TextTypeTest extends TestCase
{
public function testSubmitNullReturnsNull()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\TextType', 'name');
$form->submit(null);
$this->assertNull($form->getData());
}
public function testSubmitNullReturnsEmptyStringWithEmptyDataAsString()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\TextType', 'name', array(
'empty_data' => '',
));
$form->submit(null);
$this->assertSame('', $form->getData());
}
}