Skip to content

Commit

Permalink
[Form] Removed separator characters between choice or text fields in …
Browse files Browse the repository at this point in the history
…DateType
  • Loading branch information
webmozart committed Jan 5, 2013
1 parent 31ff3db commit e0b4480
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* TrimListener now removes unicode whitespaces
* deprecated getParent(), setParent() and hasParent() in FormBuilderInterface
* 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
-----
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -137,13 +137,18 @@ public function finishView(FormView $view, FormInterface $form, array $options)
if ($form->getConfig()->hasAttribute('formatter')) {
$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)
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
} else {
// default fallback
$pattern = '{{ year }}-{{ month }}-{{ day }}';
$pattern = '{{ year }}{{ month }}{{ day }}';
}

$view->vars['date_pattern'] = $pattern;
Expand Down
Expand Up @@ -24,21 +24,21 @@ protected function setUp()
}

/**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testInvalidWidgetOption()
{
$form = $this->factory->create('date', null, array(
$this->factory->create('date', null, array(
'widget' => 'fake_widget',
));
}

/**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testInvalidInputOption()
{
$form = $this->factory->create('date', null, array(
$this->factory->create('date', null, array(
'input' => 'fake_input',
));
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public function testSubmitFromInputRawDifferentPattern()
* 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.
*
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfFormatIsNoPattern()
{
Expand All @@ -283,7 +283,7 @@ public function testThrowExceptionIfFormatIsNoPattern()
}

/**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
{
Expand All @@ -294,7 +294,7 @@ public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
}

/**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfFormatIsNoConstant()
{
Expand All @@ -304,7 +304,7 @@ public function testThrowExceptionIfFormatIsNoConstant()
}

/**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfFormatIsInvalid()
{
Expand Down Expand Up @@ -510,7 +510,7 @@ public function testPassDatePatternToView()
$form = $this->factory->create('date');
$view = $form->createView();

$this->assertSame('{{ day }}.{{ month }}.{{ year }}', $view->vars['date_pattern']);
$this->assertSame('{{ day }}{{ month }}{{ year }}', $view->vars['date_pattern']);
}

public function testPassDatePatternToViewDifferentFormat()
Expand All @@ -521,10 +521,21 @@ public function testPassDatePatternToViewDifferentFormat()

$view = $form->createView();

$this->assertSame('{{ day }}. {{ month }} {{ year }}', $view->vars['date_pattern']);
$this->assertSame('{{ day }}{{ month }}{{ year }}', $view->vars['date_pattern']);
}

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(
'format' => 'MM*yyyy*dd'
Expand Down

0 comments on commit e0b4480

Please sign in to comment.