8000 bug #37837 [Form] Fix custom formats deprecation with HTML5 widgets (… · symfony/symfony@9e7e2a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e7e2a8

Browse files
committed
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 | #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 ------- d28182f [Form] Fix custom formats deprecation with HTML5 widgets
2 parents 8611b0b + d28182f commit 9e7e2a8

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Form\FormView;
2626
use Symfony\Component\Form\ReversedTransformer;
2727
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
28+
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
2829
use Symfony\Component\OptionsResolver\Options;
2930
use Symfony\Component\OptionsResolver\OptionsResolver;
3031

@@ -344,14 +345,23 @@ public function configureOptions(OptionsResolver $resolver)
344345

345346
return '';
346347
});
347-
$resolver->setDeprecated('html5', function (Options $options, $html5) {
348-
if ($html5 && self::HTML5_FORMAT !== $options['format']) {
349-
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);
350-
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
351-
}
352-
353-
return '';
354-
});
348+
foreach (['html5', 'format'] as $option) {
349+
$resolver->setDeprecated($option, static function (Options $options, $value) use ($option): string {
350+
try {
351+
$html5 = 'html5' === $option ? $value : $options['html5'];
352+
$format = 'format' === $option ? $value : $options['format'];
353+
} catch (OptionDefinitionException $e) {
354+
return '';
355+
}
356+
357+
if ($html5 && self::HTML5_FORMAT !== $format) {
358+
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);
359+
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
360+
}
361+
362+
return '';
363+
});
364+
}
355365
}
356366

357367
/**

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Form\FormView;
2323
use Symfony\Component\Form\ReversedTransformer;
2424
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
25+
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
2526
use Symfony\Component\OptionsResolver\Options;
2627
use Symfony\Component\OptionsResolver\OptionsResolver;
2728

@@ -322,14 +323,24 @@ public function configureOptions(OptionsResolver $resolver)
322323
$resolver->setAllowedTypes('days', 'array');
323324
$resolver->setAllowedTypes('input_format', 'string');
324325

325-
$resolver->setDeprecated('html5', function (Options $options, $html5) {
326-
if ($html5 && 'single_text' === $options['widget'] && self::HTML5_FORMAT !== $options['format']) {
327-
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);
328-
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
329-
}
326+
foreach (['html5', 'widget', 'format'] as $option) {
327+
$resolver->setDeprecated($option, static function (Options $options, $value) use ($option): string {
328+
try {
329+
$html5 = 'html5' === $option ? $value : $options['html5'];
330+
$widget = 'widget' === $option ? $value : $options['widget'];
331+
$format = 'format' === $option ? $value : $options['format'];
332+
} catch (OptionDefinitionException $e) {
333+
return '';
334+
}
330335

331-
return '';
332-
});
336+
if ($html5 && 'single_text' === $widget && self::HTML5_FORMAT !== $format) {
337+
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);
338+
//throw new LogicException(sprintf('Cannot use the "format" option of "%s" when the "html5" option is disabled.', self::class));
339+
}
340+
341+
return '';
342+
});
343+
}
333344
}
334345

335346
/**

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ public function testSingleTextWidgetWithCustomNonHtml5Format()
532532
'widget' => 'single_text',
533533
'date_format' => \IntlDateFormatter::SHORT,
534534
'format' => null,
535+
'html5' => false,
535536
]);
536537
$view = $form->createView();
537538

0 commit comments

Comments
 (0)
0