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

Skip to content

Commit 1d1f166

Browse files
committed
bug #21063 [Form] Fixed DateType format option for single text widget (HeahDude)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Fixed DateType format option for single text widget | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ It's currently not possible to use a custom format with `DateType` when not using one of the three values day, month or year (i.e in my case "MM/yyyy"). The formatter handles it, it looks like this option check is wrong, this PR fixes it. Commits ------- 9e0d531 [Form] Fixed DateType format option
2 parents f0d13f4 + 9e0d531 commit 1d1f166

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

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

Lines 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

Lines changed: 29 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_DE", 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,18 @@ 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+
'widget' => 'single_text',
374+
'format' => 'wrong',
375+
));
376+
}
377+
349378
/**
350379
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
351380
*/

0 commit comments

Comments
 (0)