Skip to content

Commit

Permalink
feature #18357 [Form] Let TextType implement `DataTransformerInterf…
Browse files Browse the repository at this point in the history
…ace` (HeahDude)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Form] Let `TextType` implement `DataTransformerInterface`

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #5906
| License       | MIT
| Doc PR        | ~

Commits
-------

7e97fbb [Form] let `TextType` implements `DataTransformerInterface`
  • Loading branch information
fabpot committed Apr 28, 2016
2 parents 461e871 + 7e97fbb commit 50f2529
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions UPGRADE-3.1.md
Expand Up @@ -19,6 +19,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.

* Using callable strings as choice options in ChoiceType has been deprecated
in favor of `PropertyPath` in Symfony 4.0 use a "\Closure" instead.
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -7,9 +7,9 @@ CHANGELOG
* deprecated the "choices_as_values" option of ChoiceType
* deprecated support for data objects that implements both `Traversable` and
`ArrayAccess` in `ResizeFormListener::preSubmit` method

* Using callable strings as choice options in `ChoiceType` has been deprecated
and will be used as `PropertyPath` instead of callable in Symfony 4.0.
* implemented `DataTransformerInterface` in `TextType`

3.0.0
-----
Expand Down
34 changes: 33 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/TextType.php
Expand Up @@ -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}
*/
Expand All @@ -33,4 +47,22 @@ public function getBlockPrefix()
{
return 'text';
}

/**
* {@inheritdoc}
*/
public function transform($data)
{
// Model data should not be transformed
return $data;
}

/**
* {@inheritdoc}
*.
*/
public function reverseTransform($data)
{
return null === $data ? '' : $data;
}
}
@@ -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());
}
}

0 comments on commit 50f2529

Please sign in to comment.