Skip to content

Commit

Permalink
[Form] Fix custom formats deprecation with HTML5 widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Sep 24, 2020
1 parent 02be26a commit d28182f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
26 changes: 18 additions & 8 deletions src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Expand Up @@ -25,6 +25,7 @@
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;

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

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));
}

return '';
});
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 '';
}

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 '';
});
}
}

/**
Expand Down
25 changes: 18 additions & 7 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -22,6 +22,7 @@
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;

Expand Down Expand Up @@ -322,14 +323,24 @@ public function configureOptions(OptionsResolver $resolver)
$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 '';
});
}
}

/**
Expand Down
Expand Up @@ -532,6 +532,7 @@ public function testSingleTextWidgetWithCustomNonHtml5Format()
'widget' => 'single_text',
'date_format' => \IntlDateFormatter::SHORT,
'format' => null,
'html5' => false,
]);
$view = $form->createView();

Expand Down

0 comments on commit d28182f

Please sign in to comment.