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

Skip to content

Commit cc7a6b2

Browse files
committed
[Form] Fixed DateType format option
1 parent ee69018 commit cc7a6b2

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
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
@@ -51,7 +51,7 @@ 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'))) {
54+
if (null !== $pattern && (false === strpos($pattern, 'y') && false === strpos($pattern, 'M') && false === strpos($pattern, 'd'))) {
5555
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
5656
}
5757

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

Lines changed: 17 additions & 1 deletion
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
@@ -342,7 +358,7 @@ public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
342358
{
343359
$this->factory->create('date', null, array(
344360
'months' => array(6, 7),
345-
'format' => 'yy',
361+
'format' => 'wrong',
346362
));
347363
}
348364

0 commit comments

Comments
 (0)