diff --git a/UPGRADE-4.1.md b/UPGRADE-4.1.md index b6b94be57aae2..8ee0286bb3d2c 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` is deprecated and will be removed in Symfony 5.0, use a `\Closure` instead. + + Before: + + ```php + 'empty_data' => 'some_function', + ``` + + After: + + ```php + 'empty_data' => function (FormInterface $form, $data) { + return some_function($form, $data); + }, + ``` + Workflow -------- diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index d4d2a49d481f6..ab71012a55c62 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 +---- + + * Support for callable strings as `empty_data` in `ChoiceType` has been removed. Use a `\Closure` instead. + + Before: + + ```php + 'empty_data' => 'some_function', + ``` + + After: + + ```php + 'empty_data' => function (FormInterface $form, $data) { + return some_function($form, $data); + }, + ``` + Workflow -------- diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 5d01cb38931c9..717a2c75d49ec 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.1.0 +----- + + * deprecated support for callable strings as `empty_data` in `ChoiceType` + 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..8980487800144 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -91,7 +91,14 @@ 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)) { + if (is_string($emptyData)) { + @trigger_error('Passing callable strings is deprecated since version 4.1 and will be removed in 5.0. You should use a "\Closure" instead.', E_USER_DEPRECATED); + } + $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..21d9f2dbc4764 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,47 @@ public function testSubmitMultipleChoiceExpandedWithEmptyData() $this->assertSame(array('test'), $form->getData()); } + /** + * @group legacy + */ + public function testSubmitSingleChoiceWithCallableEmptyData() + { + $form = $this->factory->create(static::TESTED_TYPE, null, array( + 'multiple' => false, + 'expanded' => true, + 'choices' => array('test'), + 'empty_data' => __CLASS__.'::emptyDataResolver', + )); + + $form->submit(null); + + $this->assertSame('test', $form->getData()); + } + + /** + * @return string + */ + public static function emptyDataResolver() + { + return 'test'; + } + + 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(