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

Commitf274178

Browse files
committed
[Form] letTextType implementsDataTransformerInterface
closes#5906.The submitted data should always betransformed back to the model as a stringas NULL in this case could stand for "unsetthis value" whereas a string property of aclass could rely on the string type.Furthermore, this prevents potential issueswith PHP 7 which allows type hinting ofstrings in functions.
1 parent2946932 commitf274178

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

‎UPGRADE-3.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Form
1818
* Support for data objects that implements both`Traversable` and`ArrayAccess`
1919
in`ResizeFormListener::preSubmit` method has been deprecated and will be
2020
removed in Symfony 4.0.
21+
*`TextType` now implements`DataTransformerInterface` and will always return
22+
an empty string when`empty_data` option is explicitly assigned to it.
2123

2224
FrameworkBundle
2325
---------------

‎src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* deprecated the "choices_as_values" option of ChoiceType
88
* deprecated support for data objects that implements both`Traversable` and
99
`ArrayAccess` in`ResizeFormListener::preSubmit` method
10+
* implemented`DataTransformerInterface` in`TextType`
1011

1112
3.0.0
1213
-----

‎src/Symfony/Component/Form/Extension/Core/Type/TextType.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,24 @@
1212
namespaceSymfony\Component\Form\Extension\Core\Type;
1313

1414
useSymfony\Component\Form\AbstractType;
15+
useSymfony\Component\Form\DataTransformerInterface;
16+
useSymfony\Component\Form\FormBuilderInterface;
1517
useSymfony\Component\OptionsResolver\OptionsResolver;
1618

17-
class TextTypeextends AbstractType
19+
class TextTypeextends AbstractTypeimplements DataTransformerInterface
1820
{
21+
publicfunctionbuildForm(FormBuilderInterface$builder,array$options)
22+
{
23+
// When empty_data is explicitly set to an empty string,
24+
// a string should always be returned when NULL is submitted
25+
// This gives more control and thus helps preventing some issues
26+
// with PHP 7 which allows type hinting strings in functions
27+
// See https://github.com/symfony/symfony/issues/5906#issuecomment-203189375
28+
if ('' ===$options['empty_data']) {
29+
$builder->addViewTransformer($this);
30+
}
31+
}
32+
1933
/**
2034
* {@inheritdoc}
2135
*/
@@ -33,4 +47,22 @@ public function getBlockPrefix()
3347
{
3448
return'text';
3549
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
publicfunctiontransform($data)
55+
{
56+
// Model data should not be transformed
57+
return$data;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*.
63+
*/
64+
publicfunctionreverseTransform($data)
65+
{
66+
returnempty($data) ?'' :$data;
67+
}
3668
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Form\Tests\Extension\Core\Type;
13+
14+
useSymfony\Component\Form\Test\TypeTestCaseasTestCase;
15+
16+
class TextTypeTestextends TestCase
17+
{
18+
publicfunctiontestSubmitNullReturnsNull()
19+
{
20+
$form =$this->factory->create('Symfony\Component\Form\Extension\Core\Type\TextType','name');
21+
22+
$form->submit(null);
23+
24+
$this->assertNull($form->getData());
25+
}
26+
27+
publicfunctiontestSubmitNullReturnsEmptyStringWithEmptyDataAsString()
28+
{
29+
$form =$this->factory->create('Symfony\Component\Form\Extension\Core\Type\TextType','name',array(
30+
'empty_data' =>'',
31+
));
32+
33+
$form->submit(null);
34+
35+
$this->assertSame('',$form->getData());
36+
}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp