File tree Expand file tree Collapse file tree 4 files changed +73
-2
lines changed
src/Symfony/Component/Form
Tests/Extension/Core/Type Expand file tree Collapse file tree 4 files changed +73
-2
lines changed Original file line number Diff line number Diff line change 1919 * Support for data objects that implements both ` Traversable ` and ` ArrayAccess `
2020 in ` ResizeFormListener::preSubmit ` method has been deprecated and will be
2121 removed in Symfony 4.0.
22+ * ` TextType ` now implements ` DataTransformerInterface ` and will always return
23+ an empty string when ` empty_data ` option is explicitly assigned to it.
2224
2325 * Using callable strings as choice options in ChoiceType has been deprecated
2426 in favor of ` PropertyPath ` in Symfony 4.0 use a "\Closure" instead.
Original file line number Diff line number Diff line change @@ -7,9 +7,9 @@ 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-
1110 * Using callable strings as choice options in ` ChoiceType ` has been deprecated
1211 and will be used as ` PropertyPath ` instead of callable in Symfony 4.0.
12+ * implemented ` DataTransformerInterface ` in ` TextType `
1313
14143.0.0
1515-----
Original file line number Diff line number Diff line change 1212namespace Symfony \Component \Form \Extension \Core \Type ;
1313
1414use Symfony \Component \Form \AbstractType ;
15+ use Symfony \Component \Form \DataTransformerInterface ;
16+ use Symfony \Component \Form \FormBuilderInterface ;
1517use Symfony \Component \OptionsResolver \OptionsResolver ;
1618
17- class TextType extends AbstractType
19+ class TextType extends AbstractType implements DataTransformerInterface
1820{
21+ public function buildForm (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+ public function transform ($ data )
55+ {
56+ // Model data should not be transformed
57+ return $ data ;
58+ }
59+
60+ /**
61+ * {@inheritdoc}
62+ *.
63+ */
64+ public function reverseTransform ($ data )
65+ {
66+ return null === $ data ? '' : $ data ;
67+ }
3668}
Original file line number Diff line number Diff line change 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+ namespace Symfony \Component \Form \Tests \Extension \Core \Type ;
13+
14+ use Symfony \Component \Form \Test \TypeTestCase as TestCase ;
15+
16+ class TextTypeTest extends TestCase
17+ {
18+ public function testSubmitNullReturnsNull ()
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+ public function testSubmitNullReturnsEmptyStringWithEmptyDataAsString ()
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+ }
You can’t perform that action at this time.
0 commit comments