[Form] Changed the default format of DateType to "yyyy-MM-dd" to support HTML 5 out of the box

This commit is contained in:
Bernhard Schussek 2012-07-10 15:57:36 +02:00
parent d621a76f28
commit 9eeb20044f
3 changed files with 72 additions and 2 deletions

View File

@ -149,3 +149,5 @@ CHANGELOG
* fixed: the "data" option supersedes default values from the model * 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 * changed DateType to refer to the "format" option for calculating the year and day choices instead
of padding them automatically 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

View File

@ -29,6 +29,8 @@ class DateType extends AbstractType
{ {
const DEFAULT_FORMAT = \IntlDateFormatter::MEDIUM; const DEFAULT_FORMAT = \IntlDateFormatter::MEDIUM;
const HTML5_FORMAT = 'yyyy-MM-dd';
private static $acceptedFormats = array( private static $acceptedFormats = array(
\IntlDateFormatter::FULL, \IntlDateFormatter::FULL,
\IntlDateFormatter::LONG, \IntlDateFormatter::LONG,
@ -130,7 +132,10 @@ class DateType extends AbstractType
{ {
$view->setVar('widget', $options['widget']); $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'); $view->setVar('type', 'date');
} }
@ -186,7 +191,7 @@ class DateType extends AbstractType
'days' => range(1, 31), 'days' => range(1, 31),
'widget' => 'choice', 'widget' => 'choice',
'input' => 'datetime', 'input' => 'datetime',
'format' => self::DEFAULT_FORMAT, 'format' => self::HTML5_FORMAT,
'data_timezone' => null, 'data_timezone' => null,
'user_timezone' => null, 'user_timezone' => null,
'empty_value' => $emptyValue, 'empty_value' => $emptyValue,

View File

@ -42,9 +42,25 @@ class DateTypeTest extends LocalizedTestCase
)); ));
} }
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() public function testSubmitFromSingleTextDateTime()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -60,6 +76,7 @@ class DateTypeTest extends LocalizedTestCase
public function testSubmitFromSingleTextString() public function testSubmitFromSingleTextString()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -75,6 +92,7 @@ class DateTypeTest extends LocalizedTestCase
public function testSubmitFromSingleTextTimestamp() public function testSubmitFromSingleTextTimestamp()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -92,6 +110,7 @@ class DateTypeTest extends LocalizedTestCase
public function testSubmitFromSingleTextRaw() public function testSubmitFromSingleTextRaw()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'single_text', 'widget' => 'single_text',
@ -296,6 +315,7 @@ class DateTypeTest extends LocalizedTestCase
public function testSetData_differentTimezones() public function testSetData_differentTimezones()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'data_timezone' => 'America/New_York', 'data_timezone' => 'America/New_York',
'user_timezone' => 'Pacific/Tahiti', 'user_timezone' => 'Pacific/Tahiti',
'input' => 'string', 'input' => 'string',
@ -310,6 +330,7 @@ class DateTypeTest extends LocalizedTestCase
public function testSetData_differentTimezonesDateTime() public function testSetData_differentTimezonesDateTime()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'data_timezone' => 'America/New_York', 'data_timezone' => 'America/New_York',
'user_timezone' => 'Pacific/Tahiti', 'user_timezone' => 'Pacific/Tahiti',
'input' => 'datetime', 'input' => 'datetime',
@ -488,6 +509,17 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date'); $form = $this->factory->create('date');
$view = $form->createView(); $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')); $this->assertSame('{{ day }}.{{ month }}.{{ year }}', $view->getVar('date_pattern'));
} }
@ -623,4 +655,35 @@ class DateTypeTest extends LocalizedTestCase
$this->assertNull($view->get('month')->getVar('empty_value')); $this->assertNull($view->get('month')->getVar('empty_value'));
$this->assertSame('Empty day', $view->get('day')->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'));
}
} }