Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb0a0a7

Browse files
committed
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
2 parents 24a07fb + 6b71513 commit fb0a0a7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
144144

145145
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
146146
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime
147-
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
147+
if (preg_match('/^([yMd]+)[^yMd]*([yMd]+)[^yMd]*([yMd]+)$/', $pattern)) {
148148
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
149149
} else {
150150
// default fallback

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,29 @@ public function testSubmitFromInputRawDifferentPattern()
267267
$this->assertEquals('06*2010*02', $form->getViewData());
268268
}
269269

270+
/**
271+
* @dataProvider provideDateFormats
272+
*/
273+
public function testDatePatternWithFormatOption($format, $pattern)
274+
{
275+
$form = $this->factory->create('date', null, array(
276+
'format' => $format,
277+
));
278+
279+
$view = $form->createView();
280+
281+
$this->assertEquals($pattern, $view->vars['date_pattern']);
282+
}
283+
284+
public function provideDateFormats()
285+
{
286+
return array(
287+
array('dMy', '{{ day }}{{ month }}{{ year }}'),
288+
array('d-M-yyyy', '{{ day }}-{{ month }}-{{ year }}'),
289+
array('M d y', '{{ month }} {{ day }} {{ year }}'),
290+
);
291+
}
292+
270293
/**
271294
* This test is to check that the strings '0', '1', '2', '3' are no accepted
272295
* as valid IntlDateFormatter constants for FULL, LONG, MEDIUM or SHORT respectively.

0 commit comments

Comments
 (0)