Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a8f8fb2

Browse filesBrowse files
committed
[Form] Fixed DateType format option
1 parent ee69018 commit a8f8fb2
Copy full SHA for a8f8fb2

File tree

Expand file treeCollapse file tree

2 files changed

+38
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+38
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/DateType.php
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5151
throw new InvalidOptionsException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom format.');
5252
}
5353

54-
if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) {
55-
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
56-
}
57-
5854
if ('single_text' === $options['widget']) {
55+
if (null !== $pattern && false === strpos($pattern, 'y') && false === strpos($pattern, 'M') && false === strpos($pattern, 'd')) {
56+
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" or "d". Its current value is "%s".', $pattern));
57+
}
58+
5959
$builder->addViewTransformer(new DateTimeToLocalizedStringTransformer(
6060
$options['model_timezone'],
6161
$options['view_timezone'],
@@ -65,6 +65,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6565
$pattern
6666
));
6767
} else {
68+
if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) {
69+
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
70+
}
71+
6872
$yearOptions = $monthOptions = $dayOptions = array(
6973
'error_bubbling' => true,
7074
);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ public function testSubmitFromSingleTextDateTimeWithDefaultFormat()
6767
$this->assertEquals('2010-06-02', $form->getViewData());
6868
}
6969

70+
public function testSubmitFromSingleTextDateTimeWithCustomFormat()
71+
{
72+
$form = $this->factory->create('date', null, array(
73+
'model_timezone' => 'UTC',
74+
'view_timezone' => 'UTC',
75+
'widget' => 'single_text',
76+
'input' => 'datetime',
77+
'format' => 'yyyy',
78+
));
79+
80+
$form->submit('2010');
81+
82+
$this->assertDateTimeEquals(new \DateTime('2010-01-01 UTC'), $form->getData());
83+
$this->assertEquals('2010', $form->getViewData());
84+
}
85+
7086
public function testSubmitFromSingleTextDateTime()
7187
{
7288
// we test against "de_AT", so we need the full implementation
@@ -337,6 +353,7 @@ public function testThrowExceptionIfFormatIsNoPattern()
337353

338354
/**
339355
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
356+
* @expectedExceptionMessage The "format" option should contain the letters "y", "M" and "d". Its current value is "yy".
340357
*/
341358
public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
342359
{
@@ -346,6 +363,19 @@ public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
346363
));
347364
}
348365

366+
/**
367+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
368+
* @expectedExceptionMessage The "format" option should contain the letters "y", "M" or "d". Its current value is "wrong".
369+
*/
370+
public function testThrowExceptionIfFormatDoesNotContainYearMonthOrDay()
371+
{
372+
$this->factory->create('date', null, array(
373+
'months' => array(6, 7),
374+
'widget' => 'single_text',
375+
'format' => 'wrong',
376+
));
377+
}
378+
349379
/**
350380
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
351381
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.