diff --git a/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php b/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php index 5249e3b36..8dd5acb61 100644 --- a/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php +++ b/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php @@ -62,7 +62,7 @@ public function reverseTransform($array) $emptyKeys = []; foreach ($this->keys as $key) { - if (isset($array[$key]) && '' !== $array[$key] && false !== $array[$key] && [] !== $array[$key]) { + if (isset($array[$key]) && false !== $array[$key] && [] !== $array[$key]) { if ($array[$key] !== $result) { throw new TransformationFailedException('All values in the array should be the same.'); } diff --git a/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php b/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php index fdfd98357..462472da9 100644 --- a/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php +++ b/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php @@ -70,7 +70,7 @@ public function testReverseTransformCompletelyEmpty() 'c' => '', ]; - $this->assertNull($this->transformer->reverseTransform($input)); + $this->assertSame('', $this->transformer->reverseTransform($input)); } public function testReverseTransformCompletelyNull() diff --git a/Tests/Extension/Core/Type/RepeatedTypeTest.php b/Tests/Extension/Core/Type/RepeatedTypeTest.php index b2a295b27..ca0de1223 100644 --- a/Tests/Extension/Core/Type/RepeatedTypeTest.php +++ b/Tests/Extension/Core/Type/RepeatedTypeTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Form\Tests\Extension\Core\Type; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Form; use Symfony\Component\Form\Tests\Fixtures\NotMappedType; use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; @@ -191,6 +192,36 @@ public function testSetOptionsPerChildAndOverwrite() $this->assertTrue($form['second']->isRequired()); } + /** + * @dataProvider emptyDataProvider + */ + public function testSubmitNullForTextTypeWithEmptyDataOptionSetToEmptyString($emptyData, $submittedData, $expected) + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'type' => TextType::class, + 'options' => [ + 'empty_data' => $emptyData, + ] + ]); + $form->submit($submittedData); + + $this->assertSame($expected, $form->getData()); + } + + public static function emptyDataProvider() + { + yield ['', null, '']; + yield ['', ['first' => null, 'second' => null], '']; + yield ['', ['first' => '', 'second' => null], '']; + yield ['', ['first' => null, 'second' => ''], '']; + yield ['', ['first' => '', 'second' => ''], '']; + yield [null, null, null]; + yield [null, ['first' => null, 'second' => null], null]; + yield [null, ['first' => '', 'second' => null], null]; + yield [null, ['first' => null, 'second' => ''], null]; + yield [null, ['first' => '', 'second' => ''], null]; + } + public function testSubmitUnequal() { $input = ['first' => 'foo', 'second' => 'bar'];