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
ignore custom Closure in "empty_data" option
  • Loading branch information
peter-gribanov authored and Peter Gribanov committed Sep 27, 2022
commit 155eb91388811a246bb95492ca69f41d11a89153
25 changes: 14 additions & 11 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
class FormType extends BaseType
{
private DataMapper $dataMapper;
private static $emptyData = null;

public function __construct(PropertyAccessorInterface $propertyAccessor = null)
{
Expand Down Expand Up @@ -139,19 +140,21 @@ public function configureOptions(OptionsResolver $resolver)
};

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

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

if (null !== $class) {
return function (FormInterface $form) use ($class) {
return $form->isEmpty() && !$form->isRequired() ? null : new $class();
return function (FormInterface $form) {
return $form->getConfig()->getCompound() ? [] : '';
};
}

return function (FormInterface $form) {
return $form->getConfig()->getCompound() ? [] : '';
};
};
}

// Wrap "post_max_size_message" in a closure to translate it lazily
$uploadMaxSizeMessage = function (Options $options) {
Expand All @@ -174,7 +177,7 @@ public function configureOptions(OptionsResolver $resolver)

$resolver->setDefaults([
'data_class' => $dataClass,
'empty_data' => $emptyData,
'empty_data' => self::$emptyData,
'trim' => true,
'required' => true,
'property_path' => null,
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/TextType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ('' === $options['empty_data']) {
$builder->addViewTransformer($this);
} elseif ($options['empty_data'] instanceof \Closure) {
// we assume that this is the default value from \Symfony\Component\Form\Extension\Core\Type\FormType::configureOptions()
trigger_deprecation('symfony/form', '5.4', 'The default value of "empty_data" option in "%s" will be changed to empty string. Declare "NULL" as value for "empty_data" if you still want use "NULL" as data.', __CLASS__);
$emptyData = new \ReflectionProperty(FormType::class, 'emptyData');
$emptyData->setAccessible(true);
// Closure in "empty_data" option is the default value from FormType::configureOptions()
if ($emptyData->getValue($builder->getForm()) === $options['empty_data']) {
trigger_deprecation('symfony/form', '5.4', 'The default value of "empty_data" option in "%s" will be changed to empty string. Declare "NULL" as value for "empty_data" if you still want use "NULL" as data.', __CLASS__);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,7 @@ public function provideEmptyData()
'Compound text field' => ['text', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
'Compound choice field' => ['choice', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
// TODO uncomment after update to Symfony 6.0
// 'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,7 @@ public function provideEmptyData()
'Compound text fields' => ['text', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
'Compound choice fields' => ['choice', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
// TODO uncomment after update to Symfony 6.0
// 'Compound text fields lazy' => ['text', $lazyEmptyData, $expectedData],
'Compound text fields lazy' => ['text', $lazyEmptyData, $expectedData],
'Compound choice fields lazy' => ['choice', $lazyEmptyData, $expectedData],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,7 @@ public function provideEmptyData()
'Compound text field' => ['text', ['hour' => '21', 'minute' => '23'], $expectedData],
'Compound choice field' => ['choice', ['hour' => '21', 'minute' => '23'], $expectedData],
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
// TODO uncomment after update to Symfony 6.0
// 'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
];
}
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