fixed date type format pattern regex

This commit is contained in:
Bilal Amarni 2013-06-05 16:35:05 +02:00 committed by Fabien Potencier
parent 24a07fbab8
commit 6b715136c8
2 changed files with 24 additions and 1 deletions

View File

@ -144,7 +144,7 @@ class DateType extends AbstractType
// 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)) {
if (preg_match('/^([yMd]+)[^yMd]*([yMd]+)[^yMd]*([yMd]+)$/', $pattern)) {
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
} else {
// default fallback

View File

@ -267,6 +267,29 @@ class DateTypeTest extends LocalizedTestCase
$this->assertEquals('06*2010*02', $form->getViewData());
}
/**
* @dataProvider provideDateFormats
*/
public function testDatePatternWithFormatOption($format, $pattern)
{
$form = $this->factory->create('date', null, array(
'format' => $format,
));
$view = $form->createView();
$this->assertEquals($pattern, $view->vars['date_pattern']);
}
public function provideDateFormats()
{
return array(
array('dMy', '{{ day }}{{ month }}{{ year }}'),
array('d-M-yyyy', '{{ day }}-{{ month }}-{{ year }}'),
array('M d y', '{{ month }} {{ day }} {{ year }}'),
);
}
/**
* 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.