merged branch bamarni/date-format-regex (PR #8198)
This PR was submitted for the 2.1 branch but it was merged into the 2.2 branch instead (closes #8198). Discussion ---------- [Form] fixed date type format pattern regex | Q | A | ------------- | --- | Bug fix? | [yes] | New feature? | [no] | BC breaks? | [no] | Deprecations? | [no] | Tests pass? | [yes] | License | MIT I don't understand what is the rationale behind the current regex, why is there mandatory in-between characters? The current regex passes with the format option set to ```dMyyyy``` while it doesn't with ```dMy```, on the linked icu documentation it is stated that ```y``` is equivalent to ```yyyy```. So when setting this format option to dMy, fields are rendered in a wrong order because of the fallback (year, month, day). Commits ------- 6a91bbb [Form] fixed date type format pattern regex
This commit is contained in:
commit
fb0a0a708b
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
Reference in New Issue