Skip to content

Commit cf1a7d8

Browse files
committed
Merge branch '2.8' into 3.2
* 2.8: 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 [Config] Fix checking cache for non existing meta file
2 parents ac9edcb + acdef61 commit cf1a7d8

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
@@ -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 testThrowExceptionIfFormatMissesYearMonthAndDayWithSingleTextWidget()
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)