Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
deprecate custom formats with HTML5 widgets
  • Loading branch information
xabbuh committed Oct 16, 2018
1 parent 97c8fac commit e547eb7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions UPGRADE-4.2.md
Expand Up @@ -55,6 +55,7 @@ Finder
Form
----

* Using the `format` option of the `DateType` and `DateTimeType` when the `html5` option is enabled is deprecated.
* The `getExtendedType()` method of the `FormTypeExtensionInterface` is deprecated and will be removed in 5.0. Type
extensions must implement the static `getExtendedTypes()` method instead and return an iterable of extended types.

Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Expand Up @@ -72,6 +72,7 @@ Finder
Form
----

* Removed support for using the `format` option of the `DateType` and `DateTimeType` when the `html5` option is enabled.
* The `getExtendedType()` method was removed from the `FormTypeExtensionInterface`. It is replaced by the the static
`getExtendedTypes()` method which must return an iterable of extended types.

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.2.0
-----

* deprecated using the `format` option of the `DateType` and `DateTimeType` when the `html5` option is enabled
* The `getExtendedType()` method of the `FormTypeExtensionInterface` is deprecated and will be removed in 5.0. Type
extensions must implement the static `getExtendedTypes()` method instead and return an iterable of extended types.

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Expand Up @@ -148,6 +148,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$dateOptions['input'] = $timeOptions['input'] = 'array';
$dateOptions['error_bubbling'] = $timeOptions['error_bubbling'] = true;

if (isset($dateOptions['format']) && DateType::HTML5_FORMAT !== $dateOptions['format']) {
$dateOptions['html5'] = false;
}

$builder
->addViewTransformer(new DataTransformerChain(array(
new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts),
Expand Down Expand Up @@ -278,6 +282,15 @@ public function configureOptions(OptionsResolver $resolver)
'text',
'choice',
));

$resolver->setNormalizer('html5', function (Options $options, $html5) {
if ($html5 && self::HTML5_FORMAT !== $options['format']) {
@trigger_error(sprintf('Using a custom format when the "html5" option of the %s is enabled is deprecated since Symfony 4.2 and will lead to an exception in 5.0..', self::class), E_USER_DEPRECATED);
//throw new LogicException(sprintf('Cannot use the "format" option of the %s when the "html5" option is disabled.', self::class));
}

return $html5;
});
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -241,7 +241,7 @@ public function configureOptions(OptionsResolver $resolver)
};

$format = function (Options $options) {
return 'single_text' === $options['widget'] ? DateType::HTML5_FORMAT : DateType::DEFAULT_FORMAT;
return 'single_text' === $options['widget'] ? self::HTML5_FORMAT : self::DEFAULT_FORMAT;
};

$resolver->setDefaults(array(
Expand Down Expand Up @@ -288,6 +288,15 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('years', 'array');
$resolver->setAllowedTypes('months', 'array');
$resolver->setAllowedTypes('days', 'array');

$resolver->setNormalizer('html5', function (Options $options, $html5) {
if ($html5 && 'single_text' === $options['widget'] && self::HTML5_FORMAT !== $options['format']) {
@trigger_error(sprintf('Using a custom format when the "html5" option of the %s is enabled is deprecated since Symfony 4.2 and will lead to an exception in 5.0..', self::class), E_USER_DEPRECATED);
//throw new LogicException(sprintf('Cannot use the "format" option of the %s when the "html5" option is disabled.', self::class));
}

return $html5;
});
}

/**
Expand Down
Expand Up @@ -470,6 +470,9 @@ public function testDontPassHtml5TypeIfHtml5NotAllowed()
$this->assertArrayNotHasKey('type', $view->vars);
}

/**
* @group legacy
*/
public function testDontPassHtml5TypeIfNotHtml5Format()
{
$view = $this->factory->create(static::TESTED_TYPE, null, array(
Expand Down
Expand Up @@ -76,6 +76,7 @@ public function testSubmitFromSingleTextDateTimeWithCustomFormat()
'widget' => 'single_text',
'input' => 'datetime',
'format' => 'yyyy',
'html5' => false,
));

$form->submit('2010');
Expand All @@ -93,6 +94,7 @@ public function testSubmitFromSingleTextDateTime()

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
Expand All @@ -114,6 +116,7 @@ public function testSubmitFromSingleTextDateTimeImmutable()

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
Expand All @@ -136,6 +139,7 @@ public function testSubmitFromSingleTextString()

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
Expand All @@ -157,6 +161,7 @@ public function testSubmitFromSingleTextTimestamp()

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
Expand All @@ -180,6 +185,7 @@ public function testSubmitFromSingleTextRaw()

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
Expand Down Expand Up @@ -270,6 +276,7 @@ public function testSubmitFromInputDateTimeDifferentPattern()
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text',
'input' => 'datetime',
));
Expand All @@ -286,6 +293,7 @@ public function testSubmitFromInputStringDifferentPattern()
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text',
'input' => 'string',
));
Expand All @@ -302,6 +310,7 @@ public function testSubmitFromInputTimestampDifferentPattern()
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text',
'input' => 'timestamp',
));
Expand All @@ -320,6 +329,7 @@ public function testSubmitFromInputRawDifferentPattern()
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'format' => 'MM*yyyy*dd',
'html5' => false,
'widget' => 'single_text',
'input' => 'array',
));
Expand Down Expand Up @@ -368,6 +378,7 @@ public function testThrowExceptionIfFormatIsNoPattern()
{
$this->factory->create(static::TESTED_TYPE, null, array(
'format' => '0',
'html5' => false,
'widget' => 'single_text',
'input' => 'string',
));
Expand All @@ -394,6 +405,7 @@ public function testThrowExceptionIfFormatMissesYearMonthAndDayWithSingleTextWid
$this->factory->create(static::TESTED_TYPE, null, array(
'widget' => 'single_text',
'format' => 'wrong',
'html5' => false,
));
}

Expand Down Expand Up @@ -456,6 +468,7 @@ public function testSetDataWithNegativeTimezoneOffsetStringInput()

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'America/New_York',
'input' => 'string',
Expand All @@ -478,6 +491,7 @@ public function testSetDataWithNegativeTimezoneOffsetDateTimeInput()

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'America/New_York',
'input' => 'datetime',
Expand Down Expand Up @@ -856,6 +870,7 @@ public function testDontPassHtml5TypeIfNotHtml5Format()
$view = $this->factory->create(static::TESTED_TYPE, null, array(
'widget' => 'single_text',
'format' => \IntlDateFormatter::MEDIUM,
'html5' => false,
))
->createView();

Expand Down

0 comments on commit e547eb7

Please sign in to comment.