diff --git a/UPGRADE-4.1.md b/UPGRADE-4.1.md index b6b94be57aae2..372b2b84316fa 100644 --- a/UPGRADE-4.1.md +++ b/UPGRADE-4.1.md @@ -45,6 +45,25 @@ Validator * Calling `EmailValidator::__construct()` method with a boolean parameter is deprecated and will be removed in 5.0, use `EmailValidator("strict")` instead. * Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint. They will be removed in 5.0. +Form +---- + + * Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead. + + Before: + + ```php + 'empty_data' => 'SomeValueObject::getDefaultValue', + ``` + + After: + + ```php + 'empty_data' => function (FormInterface $form, $data) { + return SomeValueObject::getDefaultValue(); + }, + ``` + Workflow -------- diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index d4d2a49d481f6..edb170c46b3eb 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -42,6 +42,25 @@ Validator * Calling `EmailValidator::__construct()` method with a boolean parameter has been removed, use `EmailValidator("strict")` instead. * Removed the `checkDNS` and `dnsMessage` options from the `Url` constraint. +Form +---- + + * Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead. + + Before: + + ```php + 'empty_data' => 'SomeValueObject::getDefaultValue', + ``` + + After: + + ```php + 'empty_data' => function (FormInterface $form, $data) { + return SomeValueObject::getDefaultValue(); + }, + ``` + Workflow -------- diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 5d01cb38931c9..33e1883310c6c 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.1.0 +----- + + * Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead. + 4.0.0 ----- diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index d9df942c6af30..18fc217a2ab63 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -91,7 +91,11 @@ public function buildForm(FormBuilderInterface $builder, array $options) $emptyData = $form->getConfig()->getEmptyData(); if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) { - $data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData; + if (is_callable($emptyData) && !is_string($emptyData)) { + $data = call_user_func($emptyData, $form, $data); + } else { + $data = $emptyData; + } } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 192d38f1a9225..18db447db3c5d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -640,6 +640,22 @@ public function testSubmitMultipleChoiceExpandedWithEmptyData() $this->assertSame(array('test'), $form->getData()); } + public function testSubmitSingleChoiceWithClosureEmptyData() + { + $form = $this->factory->create(static::TESTED_TYPE, null, array( + 'multiple' => false, + 'expanded' => true, + 'choices' => array('test'), + 'empty_data' => function () { + return 'test'; + }, + )); + + $form->submit(null); + + $this->assertSame('test', $form->getData()); + } + public function testSubmitMultipleNonExpanded() { $form = $this->factory->create(static::TESTED_TYPE, null, array(