Skip to content

[Form] Add upgrade path for empty_data option in TextType #41516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: 7.4
Choose a base branch
from
Prev Previous commit
Next Next commit
correct catch deprecation
  • Loading branch information
peter-gribanov authored and Peter Gribanov committed Sep 27, 2022
commit 0c10e77e9da60b501d28dc10af07dd95848097e4
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ public function configureOptions(OptionsResolver $resolver)
};

// Derive "empty_data" closure from "data_class" option
$emptyData = static function (Options $options): callable {
$emptyData = function (Options $options) {
$class = $options['data_class'];

if (null !== $class) {
return static function (FormInterface $form) use ($class): ?object {
return function (FormInterface $form) use ($class) {
return $form->isEmpty() && !$form->isRequired() ? null : new $class();
};
}

return static function (FormInterface $form) {
return function (FormInterface $form) {
return $form->getConfig()->getCompound() ? [] : '';
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// See https://github.com/symfony/symfony/issues/5906#issuecomment-203189375
if ('' === $options['empty_data']) {
$builder->addViewTransformer($this);
} elseif (is_callable($options['empty_data'])) {
} elseif ($options['empty_data'] instanceof \Closure) {
$scope = (new \ReflectionFunction($options['empty_data']))->getClosureScopeClass();

if (null !== $scope && FormType::class === $scope->getName()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

class TextTypeTest extends BaseTypeTest
{
use ExpectDeprecationTrait;

public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\TextType';
public const TESTED_TYPE_OPTIONS = [
'empty_data' => null,
Expand All @@ -38,8 +42,10 @@ public function testSubmitNullReturnsNullWithEmptyDataAsString()
/**
* @group legacy
*/
public function testDefaultEmptyDataCallback(): void
public function testDefaultEmptyDataCallback()
{
$this->expectDeprecation('Since symfony/form 5.4: The default value of "empty_data" option in "Symfony\Component\Form\Extension\Core\Type\TextType" will be changed to empty string. Declare "NULL" as value for "empty_data" if you still want use "NULL" as data.');

$form = $this->factory->create(static::TESTED_TYPE);

$form->submit(null);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy