Skip to content

Commit 1988f54

Browse files
committed
[Form] Fix custom formats deprecation with HTML5 widgets
1 parent 33acf4a commit 1988f54

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

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
/**

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
/**

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)