From 9eeb20044f876d4aec40f3946b32b4b2a38e3690 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 10 Jul 2012 15:57:36 +0200 Subject: [PATCH] [Form] Changed the default format of DateType to "yyyy-MM-dd" to support HTML 5 out of the box --- src/Symfony/Component/Form/CHANGELOG.md | 2 + .../Form/Extension/Core/Type/DateType.php | 9 ++- .../Extension/Core/Type/DateTypeTest.php | 63 +++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 54efc1dd295e..722d031b7ab7 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -149,3 +149,5 @@ CHANGELOG * fixed: the "data" option supersedes default values from the model * changed DateType to refer to the "format" option for calculating the year and day choices instead of padding them automatically + * [BC BREAK] DateType defaults to the format "yyyy-MM-dd" now in order to support + the HTML 5 date field out of the box diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php index bd3b16f822de..4d86fdac4b57 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php @@ -29,6 +29,8 @@ class DateType extends AbstractType { const DEFAULT_FORMAT = \IntlDateFormatter::MEDIUM; + const HTML5_FORMAT = 'yyyy-MM-dd'; + private static $acceptedFormats = array( \IntlDateFormatter::FULL, \IntlDateFormatter::LONG, @@ -130,7 +132,10 @@ public function finishView(FormViewInterface $view, FormInterface $form, array $ { $view->setVar('widget', $options['widget']); - if ('single_text' === $options['widget']) { + // Change the input to a HTML5 date input if + // * the widget is set to "single_text" + // * the format matches the one expected by HTML5 + if ('single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) { $view->setVar('type', 'date'); } @@ -186,7 +191,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) 'days' => range(1, 31), 'widget' => 'choice', 'input' => 'datetime', - 'format' => self::DEFAULT_FORMAT, + 'format' => self::HTML5_FORMAT, 'data_timezone' => null, 'user_timezone' => null, 'empty_value' => $emptyValue, diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php index d6ddd814d374..f1f117ce26dd 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php @@ -42,9 +42,25 @@ public function testInvalidInputOption() )); } + public function testSubmitFromSingleTextDateTimeWithDefaultFormat() + { + $form = $this->factory->create('date', null, array( + 'data_timezone' => 'UTC', + 'user_timezone' => 'UTC', + 'widget' => 'single_text', + 'input' => 'datetime', + )); + + $form->bind('2010-06-02'); + + $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $form->getData()); + $this->assertEquals('2010-06-02', $form->getViewData()); + } + public function testSubmitFromSingleTextDateTime() { $form = $this->factory->create('date', null, array( + 'format' => \IntlDateFormatter::MEDIUM, 'data_timezone' => 'UTC', 'user_timezone' => 'UTC', 'widget' => 'single_text', @@ -60,6 +76,7 @@ public function testSubmitFromSingleTextDateTime() public function testSubmitFromSingleTextString() { $form = $this->factory->create('date', null, array( + 'format' => \IntlDateFormatter::MEDIUM, 'data_timezone' => 'UTC', 'user_timezone' => 'UTC', 'widget' => 'single_text', @@ -75,6 +92,7 @@ public function testSubmitFromSingleTextString() public function testSubmitFromSingleTextTimestamp() { $form = $this->factory->create('date', null, array( + 'format' => \IntlDateFormatter::MEDIUM, 'data_timezone' => 'UTC', 'user_timezone' => 'UTC', 'widget' => 'single_text', @@ -92,6 +110,7 @@ public function testSubmitFromSingleTextTimestamp() public function testSubmitFromSingleTextRaw() { $form = $this->factory->create('date', null, array( + 'format' => \IntlDateFormatter::MEDIUM, 'data_timezone' => 'UTC', 'user_timezone' => 'UTC', 'widget' => 'single_text', @@ -296,6 +315,7 @@ public function testThrowExceptionIfFormatIsInvalid() public function testSetData_differentTimezones() { $form = $this->factory->create('date', null, array( + 'format' => \IntlDateFormatter::MEDIUM, 'data_timezone' => 'America/New_York', 'user_timezone' => 'Pacific/Tahiti', 'input' => 'string', @@ -310,6 +330,7 @@ public function testSetData_differentTimezones() public function testSetData_differentTimezonesDateTime() { $form = $this->factory->create('date', null, array( + 'format' => \IntlDateFormatter::MEDIUM, 'data_timezone' => 'America/New_York', 'user_timezone' => 'Pacific/Tahiti', 'input' => 'datetime', @@ -488,6 +509,17 @@ public function testPassDatePatternToView() $form = $this->factory->create('date'); $view = $form->createView(); + $this->assertSame('{{ year }}-{{ month }}-{{ day }}', $view->getVar('date_pattern')); + } + + public function testPassDatePatternToViewDifferentFormat() + { + $form = $this->factory->create('date', null, array( + 'format' => \IntlDateFormatter::MEDIUM, + )); + + $view = $form->createView(); + $this->assertSame('{{ day }}.{{ month }}.{{ year }}', $view->getVar('date_pattern')); } @@ -623,4 +655,35 @@ public function testPassEmptyValueAsPartialArray_addNullIfRequired() $this->assertNull($view->get('month')->getVar('empty_value')); $this->assertSame('Empty day', $view->get('day')->getVar('empty_value')); } + + public function testPassHtml5TypeIfSingleTextAndHtml5Format() + { + $form = $this->factory->create('date', null, array( + 'widget' => 'single_text', + )); + + $view = $form->createView(); + $this->assertSame('date', $view->getVar('type')); + } + + public function testDontPassHtml5TypeIfNotHtml5Format() + { + $form = $this->factory->create('date', null, array( + 'widget' => 'single_text', + 'format' => \IntlDateFormatter::MEDIUM, + )); + + $view = $form->createView(); + $this->assertNull($view->getVar('type')); + } + + public function testPassHtml5TypeIfNotSingleText() + { + $form = $this->factory->create('date', null, array( + 'widget' => 'text', + )); + + $view = $form->createView(); + $this->assertNull($view->getVar('type')); + } }