[Form] Fix custom formats deprecation with HTML5 widgets
This commit is contained in:
parent
02be26a39c
commit
d28182f99b
@ -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 '';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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 '';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -532,6 +532,7 @@ class DateTimeTypeTest extends BaseTypeTest
|
||||
'widget' => 'single_text',
|
||||
'date_format' => \IntlDateFormatter::SHORT,
|
||||
'format' => null,
|
||||
'html5' => false,
|
||||
]);
|
||||
$view = $form->createView();
|
||||
|
||||
|
Reference in New Issue
Block a user