bug #37837 [Form] Fix custom formats deprecation with HTML5 widgets (fancyweb)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] Fix custom formats deprecation with HTML5 widgets

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | https://github.com/symfony/symfony/issues/37698
| License       | MIT
| Doc PR        | -

1. The options resolver only show the deprecations for user defined (overidden) options.
2. The default value of the `html5` option is enough to pass the logical condition of the callback.

That means that only setting the `format` option (like in the reproducer) does not trigger the deprecation while it should. I think we need a feature in the options resolver component to handle those kind of cases 🤷‍♂️

Meanwhile, we can fix the issue by "deprecating" all the concerned options of the logical condition of the callback.

Commits
-------

d28182f99b [Form] Fix custom formats deprecation with HTML5 widgets
This commit is contained in:
Fabien Potencier 2020-09-24 12:57:15 +02:00
commit 9e7e2a87c9
3 changed files with 36 additions and 14 deletions

View File

@ -25,6 +25,7 @@ use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\ReversedTransformer;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
@ -344,14 +345,23 @@ class DateTimeType extends AbstractType
return '';
});
$resolver->setDeprecated('html5', function (Options $options, $html5) {
if ($html5 && self::HTML5_FORMAT !== $options['format']) {
return sprintf('Using a custom format when the "html5" option of %s is enabled is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
}
foreach (['html5', 'format'] as $option) {
$resolver->setDeprecated($option, static function (Options $options, $value) use ($option): string {
try {
$html5 = 'html5' === $option ? $value : $options['html5'];
$format = 'format' === $option ? $value : $options['format'];
} catch (OptionDefinitionException $e) {
return '';
}
return '';
});
if ($html5 && self::HTML5_FORMAT !== $format) {
return sprintf('Using a custom format when the "html5" option of %s is enabled is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
}
return '';
});
}
}
/**

View File

@ -22,6 +22,7 @@ use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\ReversedTransformer;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
@ -322,14 +323,24 @@ class DateType extends AbstractType
$resolver->setAllowedTypes('days', 'array');
$resolver->setAllowedTypes('input_format', 'string');
$resolver->setDeprecated('html5', function (Options $options, $html5) {
if ($html5 && 'single_text' === $options['widget'] && self::HTML5_FORMAT !== $options['format']) {
return sprintf('Using a custom format when the "html5" option of %s is enabled is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
}
foreach (['html5', 'widget', 'format'] as $option) {
$resolver->setDeprecated($option, static function (Options $options, $value) use ($option): string {
try {
$html5 = 'html5' === $option ? $value : $options['html5'];
$widget = 'widget' === $option ? $value : $options['widget'];
$format = 'format' === $option ? $value : $options['format'];
} catch (OptionDefinitionException $e) {
return '';
}
return '';
});
if ($html5 && 'single_text' === $widget && self::HTML5_FORMAT !== $format) {
return sprintf('Using a custom format when the "html5" option of %s is enabled is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
}
return '';
});
}
}
/**

View File

@ -532,6 +532,7 @@ class DateTimeTypeTest extends BaseTypeTest
'widget' => 'single_text',
'date_format' => \IntlDateFormatter::SHORT,
'format' => null,
'html5' => false,
]);
$view = $form->createView();