Skip to content

Commit acdef61

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: Remove 3.1 from PR template fixed test name Casting TableCell value to string. [FrameworkBundle] fixed custom domain for translations in php templates [Form] Fixed DateType format option
2 parents e545b7c + 5f2f335 commit acdef61

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Extension/Core/Type/DateType.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5656
throw new InvalidOptionsException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom format.');
5757
}
5858

59-
if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) {
60-
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
61-
}
62-
6359
if ('single_text' === $options['widget']) {
60+
if (null !== $pattern && false === strpos($pattern, 'y') && false === strpos($pattern, 'M') && false === strpos($pattern, 'd')) {
61+
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" or "d". Its current value is "%s".', $pattern));
62+
}
63+
6464
$builder->addViewTransformer(new DateTimeToLocalizedStringTransformer(
6565
$options['model_timezone'],
6666
$options['view_timezone'],
@@ -70,6 +70,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7070
$pattern
7171
));
7272
} else {
73+
if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) {
74+
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
75+
}
76+
7377
$yearOptions = $monthOptions = $dayOptions = array(
7478
'error_bubbling' => true,
7579
);

Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ public function testSubmitFromSingleTextDateTimeWithDefaultFormat()
7777
$this->assertEquals('2010-06-02', $form->getViewData());
7878
}
7979

80+
public function testSubmitFromSingleTextDateTimeWithCustomFormat()
81+
{
82+
$form = $this->factory->create('date', null, array(
83+
'model_timezone' => 'UTC',
84+
'view_timezone' => 'UTC',
85+
'widget' => 'single_text',
86+
'input' => 'datetime',
87+
'format' => 'yyyy',
88+
));
89+
90+
$form->submit('2010');
91+
92+
$this->assertDateTimeEquals(new \DateTime('2010-01-01 UTC'), $form->getData());
93+
$this->assertEquals('2010', $form->getViewData());
94+
}
95+
8096
public function testSubmitFromSingleTextDateTime()
8197
{
8298
// we test against "de_DE", so we need the full implementation
@@ -347,6 +363,7 @@ public function testThrowExceptionIfFormatIsNoPattern()
347363

348364
/**
349365
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
366+
* @expectedExceptionMessage The "format" option should contain the letters "y", "M" and "d". Its current value is "yy".
350367
*/
351368
public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
352369
{
@@ -356,6 +373,18 @@ public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
356373
));
357374
}
358375

376+
/**
377+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
378+
* @expectedExceptionMessage The "format" option should contain the letters "y", "M" or "d". Its current value is "wrong".
379+
*/
380+
public function testThrowExceptionIfFormatMissesYearMonthAndDayWithSingleTextWidget()
381+
{
382+
$this->factory->create('date', null, array(
383+
'widget' => 'single_text',
384+
'format' => 'wrong',
385+
));
386+
}
387+
359388
/**
360389
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
361390
*/

0 commit comments

Comments
 (0)