From b8ac58c56ddf9687e6dd8c2573f2ae13070526c3 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 11:35:56 +0300 Subject: [PATCH 1/8] mark usage of callable empty_data not instance of \Closure is deprecated --- .../Component/Form/Extension/Core/Type/ChoiceType.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index d9df942c6af30..920f549cf3205 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 ($emptyData instanceof \Closure) { + $data = call_user_func($emptyData, $form, $data); + } elseif (is_callable($emptyData)) { + @trigger_error('Usage of callable empty_data not instance of Closure is deprecated since version 4.1 and will be removed in 5.0.', E_USER_DEPRECATED); + $data = call_user_func($emptyData, $form, $data); + } else { + $data = $emptyData; + } } } From f8cc14441167428609f52f46976c6eb4f27e2e2c Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 11:59:14 +0300 Subject: [PATCH 2/8] allow use array as handler --- .../Component/Form/Extension/Core/Type/ChoiceType.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 920f549cf3205..3b90ac5bb4978 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -91,10 +91,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) $emptyData = $form->getConfig()->getEmptyData(); if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) { - if ($emptyData instanceof \Closure) { - $data = call_user_func($emptyData, $form, $data); - } elseif (is_callable($emptyData)) { - @trigger_error('Usage of callable empty_data not instance of Closure is deprecated since version 4.1 and will be removed in 5.0.', E_USER_DEPRECATED); + if (is_callable($emptyData)) { + if (is_string($emptyData)) { + @trigger_error('Passing callable strings is deprecated is deprecated since version 4.1 and will be removed in 5.0.', E_USER_DEPRECATED); + } $data = call_user_func($emptyData, $form, $data); } else { $data = $emptyData; From 62d2c14c66c2f143f5642e956f94859a2642a782 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 12:54:22 +0300 Subject: [PATCH 3/8] add info in UPGRADE and CHANGELOG. Add callable empty_data tests --- UPGRADE-4.1.md | 19 +++++++++ UPGRADE-5.0.md | 19 +++++++++ src/Symfony/Component/Form/CHANGELOG.md | 5 +++ .../Form/Extension/Core/Type/ChoiceType.php | 2 +- .../Extension/Core/Type/ChoiceTypeTest.php | 41 +++++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) 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 3b90ac5bb4978..8980487800144 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -93,7 +93,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) { if (is_callable($emptyData)) { if (is_string($emptyData)) { - @trigger_error('Passing callable strings is deprecated is deprecated since version 4.1 and will be removed in 5.0.', E_USER_DEPRECATED); + @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 { 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..e989e766feb5f 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( From da3c6acddbc71e23fbc51f76df402cda71677dab Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 12:56:48 +0300 Subject: [PATCH 4/8] fix CS --- .../Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e989e766feb5f..21d9f2dbc4764 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -671,7 +671,7 @@ public function testSubmitSingleChoiceWithClosureEmptyData() 'multiple' => false, 'expanded' => true, 'choices' => array('test'), - 'empty_data' => function() { + 'empty_data' => function () { return 'test'; }, )); From db28aef9e901ab7ce7e5602e6d1215726daceb43 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 13:58:43 +0300 Subject: [PATCH 5/8] use a global function in examples --- UPGRADE-4.1.md | 4 ++-- UPGRADE-5.0.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/UPGRADE-4.1.md b/UPGRADE-4.1.md index 372b2b84316fa..61d9f36bd8138 100644 --- a/UPGRADE-4.1.md +++ b/UPGRADE-4.1.md @@ -53,14 +53,14 @@ Form Before: ```php - 'empty_data' => 'SomeValueObject::getDefaultValue', + 'empty_data' => 'some_function', ``` After: ```php 'empty_data' => function (FormInterface $form, $data) { - return SomeValueObject::getDefaultValue(); + return some_function($form, $data); }, ``` diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index edb170c46b3eb..82bc6038c40da 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -50,14 +50,14 @@ Form Before: ```php - 'empty_data' => 'SomeValueObject::getDefaultValue', + 'empty_data' => 'some_function', ``` After: ```php 'empty_data' => function (FormInterface $form, $data) { - return SomeValueObject::getDefaultValue(); + return some_function($form, $data); }, ``` From 734fce146f94a03cbc7e8ca2af21bab79cb000a1 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 14:07:37 +0300 Subject: [PATCH 6/8] change description of this change in UPGRADE-5.0.md --- UPGRADE-5.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 82bc6038c40da..ab71012a55c62 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -45,7 +45,7 @@ Validator Form ---- - * Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead. + * Support for callable strings as `empty_data` in `ChoiceType` has been removed. Use a `\Closure` instead. Before: From 3d19d312b8b76d3c86a62b1926071389d4ca36b2 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 14:08:28 +0300 Subject: [PATCH 7/8] change description of this change in UPGRADE-4.1.md --- UPGRADE-4.1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADE-4.1.md b/UPGRADE-4.1.md index 61d9f36bd8138..8ee0286bb3d2c 100644 --- a/UPGRADE-4.1.md +++ b/UPGRADE-4.1.md @@ -48,7 +48,7 @@ Validator Form ---- - * Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead. + * Using callable strings as `empty_data` in `ChoiceType` is deprecated and will be removed in Symfony 5.0, use a `\Closure` instead. Before: From 6173596788c2d0b185d32c38aa5840a5af5fc61c Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 25 Jan 2018 16:44:50 +0300 Subject: [PATCH 8/8] change description of this change in CHANGELOG.md --- src/Symfony/Component/Form/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 33e1883310c6c..717a2c75d49ec 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -4,7 +4,7 @@ CHANGELOG 4.1.0 ----- - * Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead. + * deprecated support for callable strings as `empty_data` in `ChoiceType` 4.0.0 -----