Skip to content

Commit

Permalink
feature #31777 [Form] remove deprecated date types options handling (…
Browse files Browse the repository at this point in the history
…xabbuh)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[Form] remove deprecated date types options handling

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

12ed0df remove deprecated date types options handling
  • Loading branch information
fabpot committed Jun 1, 2019
2 parents 371dfc7 + 12ed0df commit 090cf32
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 62 deletions.
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\Extension\Core\Type\PercentType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormTypeExtensionInterface;
use Symfony\Component\Form\Tests\AbstractLayoutTest;

abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
Expand Down Expand Up @@ -1693,6 +1694,10 @@ public function testDateTimeWithWidgetSingleText()
*/
public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
{
if (method_exists(FormTypeExtensionInterface::class, 'getExtendedTypes')) {
$this->markTestSkipped('The test requires symfony/form 4.x.');
}

$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\DateTimeType', '2011-02-03 04:05:06', [
'input' => 'string',
'date_widget' => 'choice',
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -4,6 +4,9 @@ CHANGELOG
5.0.0
-----

* Using the `date_format`, `date_widget`, and `time_widget` options of the `DateTimeType` when the `widget` option is
set to `single_text` is not supported anymore.
* The `format` option of `DateType` and `DateTimeType` cannot be used when the `html5` option is enabled.
* Using names for buttons that do not start with a letter, a digit, or an underscore throw an exception
* Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons throw an exception.
* removed the `ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
Expand Down
33 changes: 13 additions & 20 deletions src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\DataTransformer\ArrayToPartsTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DataTransformerChain;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeImmutableToDateTimeTransformer;
Expand Down Expand Up @@ -159,10 +160,6 @@ 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([
new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts),
Expand Down Expand Up @@ -300,37 +297,33 @@ public function configureOptions(OptionsResolver $resolver)

$resolver->setAllowedTypes('input_format', 'string');

$resolver->setDeprecated('date_format', function (Options $options, $dateFormat) {
$resolver->setNormalizer('date_format', function (Options $options, $dateFormat) {
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);
//throw new LogicException(sprintf('Cannot use the "date_format" option of the %s with an HTML5 date.', self::class));
throw new LogicException(sprintf('Cannot use the "date_format" option of the %s with an HTML5 date.', self::class));
}

return '';
return $dateFormat;
});
$resolver->setDeprecated('date_widget', function (Options $options, $dateWidget) {
$resolver->setNormalizer('date_widget', function (Options $options, $dateWidget) {
if (null !== $dateWidget && 'single_text' === $options['widget']) {
return sprintf('Using the "date_widget" option of %s when the "widget" option is set to "single_text" is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
//throw new LogicException(sprintf('Cannot use the "date_widget" option of the %s when the "widget" option is set to "single_text".', self::class));
throw new LogicException(sprintf('Cannot use the "date_widget" option of the %s when the "widget" option is set to "single_text".', self::class));
}

return '';
return $dateWidget;
});
$resolver->setDeprecated('time_widget', function (Options $options, $timeWidget) {
$resolver->setNormalizer('time_widget', function (Options $options, $timeWidget) {
if (null !== $timeWidget && 'single_text' === $options['widget']) {
return sprintf('Using the "time_widget" option of %s when the "widget" option is set to "single_text" is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
//throw new LogicException(sprintf('Cannot use the "time_widget" option of the %s when the "widget" option is set to "single_text".', self::class));
throw new LogicException(sprintf('Cannot use the "time_widget" option of the %s when the "widget" option is set to "single_text".', self::class));
}

return '';
return $timeWidget;
});
$resolver->setDeprecated('html5', function (Options $options, $html5) {
$resolver->setNormalizer('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));
throw new LogicException(sprintf('Cannot use the "format" option of %s when the "html5" option is disabled.', self::class));
}

return '';
return $html5;
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeImmutableToDateTimeTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer;
Expand Down Expand Up @@ -308,13 +309,12 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('days', 'array');
$resolver->setAllowedTypes('input_format', 'string');

$resolver->setDeprecated('html5', function (Options $options, $html5) {
$resolver->setNormalizer('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));
throw new LogicException(sprintf('Cannot use the "format" option of %s when the "html5" option is disabled.', self::class));
}

return '';
return $html5;
});
}

Expand Down
23 changes: 0 additions & 23 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Expand Up @@ -1509,29 +1509,6 @@ public function testDateTimeWithWidgetSingleText()
);
}

/**
* @group legacy
*/
public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\DateTimeType', '2011-02-03 04:05:06', [
'input' => 'string',
'date_widget' => 'choice',
'time_widget' => 'choice',
'widget' => 'single_text',
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
]);

$this->assertWidgetMatchesXpath($form->createView(), [],
'/input
[@type="datetime-local"]
[@name="name"]
[@value="2011-02-03T04:05:06"]
'
);
}

public function testDateChoice()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\DateType', date('Y').'-02-03', [
Expand Down
Expand Up @@ -45,7 +45,7 @@ public function testDebugDeprecatedDefaults()
Built-in form types (Symfony\Component\Form\Extension\Core\Type)
----------------------------------------------------------------
BirthdayType, DateTimeType, DateType, IntegerType
IntegerType
Service form types
------------------
Expand Down
Expand Up @@ -301,6 +301,7 @@ public function testSubmitStringSingleTextWithSeconds()
public function testSubmitDifferentPattern()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'html5' => false,
'date_format' => 'MM*yyyy*dd',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
Expand Down Expand Up @@ -470,20 +471,6 @@ public function testDontPassHtml5TypeIfHtml5NotAllowed()
$this->assertArrayNotHasKey('type', $view->vars);
}

/**
* @group legacy
*/
public function testDontPassHtml5TypeIfNotHtml5Format()
{
$view = $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'single_text',
'format' => 'yyyy-MM-dd HH:mm',
])
->createView();

$this->assertArrayNotHasKey('type', $view->vars);
}

public function testDontPassHtml5TypeIfNotSingleText()
{
$view = $this->factory->create(static::TESTED_TYPE, null, [
Expand All @@ -497,6 +484,7 @@ public function testDontPassHtml5TypeIfNotSingleText()
public function testSingleTextWidgetWithCustomNonHtml5Format()
{
$form = $this->factory->create(static::TESTED_TYPE, new \DateTime('2019-02-13 19:12:13'), [
'html5' => false,
'widget' => 'single_text',
'date_format' => \IntlDateFormatter::SHORT,
'format' => null,
Expand Down

0 comments on commit 090cf32

Please sign in to comment.