diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php index 8af7a96f13cd6..2bc433f8414f2 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php @@ -51,11 +51,11 @@ public function buildForm(FormBuilderInterface $builder, array $options) throw new InvalidOptionsException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom format.'); } - if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) { - throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern)); - } - if ('single_text' === $options['widget']) { + if (null !== $pattern && false === strpos($pattern, 'y') && false === strpos($pattern, 'M') && false === strpos($pattern, 'd')) { + throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" or "d". Its current value is "%s".', $pattern)); + } + $builder->addViewTransformer(new DateTimeToLocalizedStringTransformer( $options['model_timezone'], $options['view_timezone'], @@ -65,6 +65,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) $pattern )); } else { + if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) { + throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern)); + } + $yearOptions = $monthOptions = $dayOptions = array( 'error_bubbling' => true, ); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php index ea302d020eec3..b31fa5e9c5fe1 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php @@ -67,6 +67,22 @@ public function testSubmitFromSingleTextDateTimeWithDefaultFormat() $this->assertEquals('2010-06-02', $form->getViewData()); } + public function testSubmitFromSingleTextDateTimeWithCustomFormat() + { + $form = $this->factory->create('date', null, array( + 'model_timezone' => 'UTC', + 'view_timezone' => 'UTC', + 'widget' => 'single_text', + 'input' => 'datetime', + 'format' => 'yyyy', + )); + + $form->submit('2010'); + + $this->assertDateTimeEquals(new \DateTime('2010-01-01 UTC'), $form->getData()); + $this->assertEquals('2010', $form->getViewData()); + } + public function testSubmitFromSingleTextDateTime() { // we test against "de_AT", so we need the full implementation @@ -337,6 +353,7 @@ public function testThrowExceptionIfFormatIsNoPattern() /** * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + * @expectedExceptionMessage The "format" option should contain the letters "y", "M" and "d". Its current value is "yy". */ public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay() { @@ -346,6 +363,18 @@ public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay() )); } + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + * @expectedExceptionMessage The "format" option should contain the letters "y", "M" or "d". Its current value is "wrong". + */ + public function testThrowExceptionIfFormatDoesNotContainYearMonthOrDay() + { + $this->factory->create('date', null, array( + 'widget' => 'single_text', + 'format' => 'wrong', + )); + } + /** * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException */