[Form] Removed separator characters between choice or text fields in DateType

This commit is contained in:
Bernhard Schussek 2012-12-06 19:58:12 +01:00
parent 31ff3db517
commit e0b4480a1b
3 changed files with 29 additions and 11 deletions

View File

@ -7,6 +7,8 @@ CHANGELOG
* TrimListener now removes unicode whitespaces * TrimListener now removes unicode whitespaces
* deprecated getParent(), setParent() and hasParent() in FormBuilderInterface * deprecated getParent(), setParent() and hasParent() in FormBuilderInterface
* FormInterface::add() now accepts a FormInterface instance OR a field's name, type and options * FormInterface::add() now accepts a FormInterface instance OR a field's name, type and options
* removed special characters between the choice or text fields of DateType unless
the option "format" is set to a custom value
2.1.0 2.1.0
----- -----

View File

@ -137,13 +137,18 @@ class DateType extends AbstractType
if ($form->getConfig()->hasAttribute('formatter')) { if ($form->getConfig()->hasAttribute('formatter')) {
$pattern = $form->getConfig()->getAttribute('formatter')->getPattern(); $pattern = $form->getConfig()->getAttribute('formatter')->getPattern();
// remove special characters unless the format was explicitly specified
if (!is_string($options['format'])) {
$pattern = preg_replace('/[^yMd]+/', '', $pattern);
}
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy) // set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime // lookup various formats at http://userguide.icu-project.org/formatparse/datetime
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) { if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern); $pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
} else { } else {
// default fallback // default fallback
$pattern = '{{ year }}-{{ month }}-{{ day }}'; $pattern = '{{ year }}{{ month }}{{ day }}';
} }
$view->vars['date_pattern'] = $pattern; $view->vars['date_pattern'] = $pattern;

View File

@ -24,21 +24,21 @@ class DateTypeTest extends LocalizedTestCase
} }
/** /**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/ */
public function testInvalidWidgetOption() public function testInvalidWidgetOption()
{ {
$form = $this->factory->create('date', null, array( $this->factory->create('date', null, array(
'widget' => 'fake_widget', 'widget' => 'fake_widget',
)); ));
} }
/** /**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/ */
public function testInvalidInputOption() public function testInvalidInputOption()
{ {
$form = $this->factory->create('date', null, array( $this->factory->create('date', null, array(
'input' => 'fake_input', 'input' => 'fake_input',
)); ));
} }
@ -271,7 +271,7 @@ class DateTypeTest extends LocalizedTestCase
* This test is to check that the strings '0', '1', '2', '3' are no accepted * This test is to check that the strings '0', '1', '2', '3' are no accepted
* as valid IntlDateFormatter constants for FULL, LONG, MEDIUM or SHORT respectively. * as valid IntlDateFormatter constants for FULL, LONG, MEDIUM or SHORT respectively.
* *
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/ */
public function testThrowExceptionIfFormatIsNoPattern() public function testThrowExceptionIfFormatIsNoPattern()
{ {
@ -283,7 +283,7 @@ class DateTypeTest extends LocalizedTestCase
} }
/** /**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/ */
public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay() public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
{ {
@ -294,7 +294,7 @@ class DateTypeTest extends LocalizedTestCase
} }
/** /**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/ */
public function testThrowExceptionIfFormatIsNoConstant() public function testThrowExceptionIfFormatIsNoConstant()
{ {
@ -304,7 +304,7 @@ class DateTypeTest extends LocalizedTestCase
} }
/** /**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/ */
public function testThrowExceptionIfFormatIsInvalid() public function testThrowExceptionIfFormatIsInvalid()
{ {
@ -510,7 +510,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date'); $form = $this->factory->create('date');
$view = $form->createView(); $view = $form->createView();
$this->assertSame('{{ day }}.{{ month }}.{{ year }}', $view->vars['date_pattern']); $this->assertSame('{{ day }}{{ month }}{{ year }}', $view->vars['date_pattern']);
} }
public function testPassDatePatternToViewDifferentFormat() public function testPassDatePatternToViewDifferentFormat()
@ -521,10 +521,21 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertSame('{{ day }}. {{ month }} {{ year }}', $view->vars['date_pattern']); $this->assertSame('{{ day }}{{ month }}{{ year }}', $view->vars['date_pattern']);
} }
public function testPassDatePatternToViewDifferentPattern() public function testPassDatePatternToViewDifferentPattern()
{
$form = $this->factory->create('date', null, array(
'format' => 'MMyyyydd'
));
$view = $form->createView();
$this->assertSame('{{ month }}{{ year }}{{ day }}', $view->vars['date_pattern']);
}
public function testPassDatePatternToViewDifferentPatternWithSeparators()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'format' => 'MM*yyyy*dd' 'format' => 'MM*yyyy*dd'