deprecate custom formats with HTML5 widgets

This commit is contained in:
Christian Flothmann 2018-10-04 13:05:56 +02:00
parent 02d6c0f22f
commit b70c1b6e0d
8 changed files with 44 additions and 3 deletions

View File

@ -23,6 +23,7 @@ Config
Form Form
---- ----
* Using the `format` option of `DateType` and `DateTimeType` when the `html5` option is enabled is deprecated.
* Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an * Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an
exception in 5.0. exception in 5.0.
* Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons is deprecated and * Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons is deprecated and

View File

@ -78,6 +78,7 @@ Finder
Form Form
---- ----
* Removed support for using the `format` option of `DateType` and `DateTimeType` when the `html5` option is enabled.
* Using names for buttons that do not start with a letter, a digit, or an underscore leads to an exception. * Using names for buttons that do not start with a letter, a digit, or an underscore leads to an exception.
* Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons leads to an * Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons leads to an
exception. exception.

View File

@ -4,6 +4,7 @@ CHANGELOG
4.3.0 4.3.0
----- -----
* Using the `format` option of `DateType` and `DateTimeType` when the `html5` option is enabled is deprecated.
* Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an * Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an
exception in 5.0. exception in 5.0.
* Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons is deprecated and * Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons is deprecated and

View File

@ -159,6 +159,10 @@ class DateTimeType extends AbstractType
$dateOptions['input'] = $timeOptions['input'] = 'array'; $dateOptions['input'] = $timeOptions['input'] = 'array';
$dateOptions['error_bubbling'] = $timeOptions['error_bubbling'] = true; $dateOptions['error_bubbling'] = $timeOptions['error_bubbling'] = true;
if (isset($dateOptions['format']) && DateType::HTML5_FORMAT !== $dateOptions['format']) {
$dateOptions['html5'] = false;
}
$builder $builder
->addViewTransformer(new DataTransformerChain([ ->addViewTransformer(new DataTransformerChain([
new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts), new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts),
@ -294,6 +298,8 @@ class DateTimeType extends AbstractType
'choice', 'choice',
]); ]);
$resolver->setAllowedTypes('input_format', 'string');
$resolver->setDeprecated('date_format', function (Options $options, $dateFormat) { $resolver->setDeprecated('date_format', function (Options $options, $dateFormat) {
if (null !== $dateFormat && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) { if (null !== $dateFormat && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
return sprintf('Using the "date_format" option of %s with an HTML5 date widget is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class); return sprintf('Using the "date_format" option of %s with an HTML5 date widget is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
@ -318,8 +324,14 @@ class DateTimeType extends AbstractType
return ''; 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));
}
$resolver->setAllowedTypes('input_format', 'string'); return '';
});
} }
/** /**

View File

@ -306,8 +306,16 @@ class DateType extends AbstractType
$resolver->setAllowedTypes('years', 'array'); $resolver->setAllowedTypes('years', 'array');
$resolver->setAllowedTypes('months', 'array'); $resolver->setAllowedTypes('months', 'array');
$resolver->setAllowedTypes('days', 'array'); $resolver->setAllowedTypes('days', 'array');
$resolver->setAllowedTypes('input_format', 'string'); $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));
}
return '';
});
} }
/** /**

View File

@ -45,7 +45,7 @@ class DebugCommandTest extends TestCase
Built-in form types (Symfony\Component\Form\Extension\Core\Type) Built-in form types (Symfony\Component\Form\Extension\Core\Type)
---------------------------------------------------------------- ----------------------------------------------------------------
DateTimeType, IntegerType, TimezoneType BirthdayType, DateTimeType, DateType, IntegerType, TimezoneType
Service form types Service form types
------------------ ------------------

View File

@ -470,6 +470,9 @@ class DateTimeTypeTest extends BaseTypeTest
$this->assertArrayNotHasKey('type', $view->vars); $this->assertArrayNotHasKey('type', $view->vars);
} }
/**
* @group legacy
*/
public function testDontPassHtml5TypeIfNotHtml5Format() public function testDontPassHtml5TypeIfNotHtml5Format()
{ {
$view = $this->factory->create(static::TESTED_TYPE, null, [ $view = $this->factory->create(static::TESTED_TYPE, null, [

View File

@ -76,6 +76,7 @@ class DateTypeTest extends BaseTypeTest
'widget' => 'single_text', 'widget' => 'single_text',
'input' => 'datetime', 'input' => 'datetime',
'format' => 'yyyy', 'format' => 'yyyy',
'html5' => false,
]); ]);
$form->submit('2010'); $form->submit('2010');
@ -93,6 +94,7 @@ class DateTypeTest extends BaseTypeTest
$form = $this->factory->create(static::TESTED_TYPE, null, [ $form = $this->factory->create(static::TESTED_TYPE, null, [
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -114,6 +116,7 @@ class DateTypeTest extends BaseTypeTest
$form = $this->factory->create(static::TESTED_TYPE, null, [ $form = $this->factory->create(static::TESTED_TYPE, null, [
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -136,6 +139,7 @@ class DateTypeTest extends BaseTypeTest
$form = $this->factory->create(static::TESTED_TYPE, null, [ $form = $this->factory->create(static::TESTED_TYPE, null, [
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -157,6 +161,7 @@ class DateTypeTest extends BaseTypeTest
$form = $this->factory->create(static::TESTED_TYPE, null, [ $form = $this->factory->create(static::TESTED_TYPE, null, [
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -180,6 +185,7 @@ class DateTypeTest extends BaseTypeTest
$form = $this->factory->create(static::TESTED_TYPE, null, [ $form = $this->factory->create(static::TESTED_TYPE, null, [
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -270,6 +276,7 @@ class DateTypeTest extends BaseTypeTest
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd', 'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text', 'widget' => 'single_text',
'input' => 'datetime', 'input' => 'datetime',
]); ]);
@ -286,6 +293,7 @@ class DateTypeTest extends BaseTypeTest
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd', 'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text', 'widget' => 'single_text',
'input' => 'string', 'input' => 'string',
]); ]);
@ -302,6 +310,7 @@ class DateTypeTest extends BaseTypeTest
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd', 'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text', 'widget' => 'single_text',
'input' => 'timestamp', 'input' => 'timestamp',
]); ]);
@ -320,6 +329,7 @@ class DateTypeTest extends BaseTypeTest
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'UTC', 'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd', 'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text', 'widget' => 'single_text',
'input' => 'array', 'input' => 'array',
]); ]);
@ -368,6 +378,7 @@ class DateTypeTest extends BaseTypeTest
{ {
$this->factory->create(static::TESTED_TYPE, null, [ $this->factory->create(static::TESTED_TYPE, null, [
'format' => '0', 'format' => '0',
'html5' => false,
'widget' => 'single_text', 'widget' => 'single_text',
'input' => 'string', 'input' => 'string',
]); ]);
@ -394,6 +405,7 @@ class DateTypeTest extends BaseTypeTest
$this->factory->create(static::TESTED_TYPE, null, [ $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'single_text', 'widget' => 'single_text',
'format' => 'wrong', 'format' => 'wrong',
'html5' => false,
]); ]);
} }
@ -456,6 +468,7 @@ class DateTypeTest extends BaseTypeTest
$form = $this->factory->create(static::TESTED_TYPE, null, [ $form = $this->factory->create(static::TESTED_TYPE, null, [
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'America/New_York', 'view_timezone' => 'America/New_York',
'input' => 'string', 'input' => 'string',
@ -478,6 +491,7 @@ class DateTypeTest extends BaseTypeTest
$form = $this->factory->create(static::TESTED_TYPE, null, [ $form = $this->factory->create(static::TESTED_TYPE, null, [
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC', 'model_timezone' => 'UTC',
'view_timezone' => 'America/New_York', 'view_timezone' => 'America/New_York',
'input' => 'datetime', 'input' => 'datetime',
@ -856,6 +870,7 @@ class DateTypeTest extends BaseTypeTest
$view = $this->factory->create(static::TESTED_TYPE, null, [ $view = $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'single_text', 'widget' => 'single_text',
'format' => \IntlDateFormatter::MEDIUM, 'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
]) ])
->createView(); ->createView();