Skip to content

Commit fbb55e7

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

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-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: 16 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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy