Skip to content

Commit

Permalink
[Form] Changed the default format of DateType to "yyyy-MM-dd" to supp…
Browse files Browse the repository at this point in the history
…ort HTML 5 out of the box
  • Loading branch information
webmozart committed Jul 10, 2012
1 parent d621a76 commit 9eeb200
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -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
9 changes: 7 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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,
Expand Down
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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'));
}

Expand Down Expand Up @@ -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'));
}
}

0 comments on commit 9eeb200

Please sign in to comment.