Permalink
Browse files

bug #21063 [Form] Fixed DateType format option for single text widget…

… (HeahDude)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Fixed DateType format option for single text widget

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~
| License       | MIT
| Doc PR        | ~

It's currently not possible to use a custom format with `DateType` when not using one of the three values day, month or year (i.e in my case "MM/yyyy").

The formatter handles it, it looks like this option check is wrong, this PR fixes it.

Commits
-------

9e0d531 [Form] Fixed DateType format option
  • Loading branch information...
2 parents f0d13f4 + 9e0d531 commit 1d1f1666e7a7106ae7573ed87a25893cd8aa545b @fabpot fabpot committed Feb 4, 2017
@@ -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,
);
@@ -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_DE", 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()
{
@@ -348,6 +365,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
*/
public function testThrowExceptionIfFormatIsNoConstant()
{

0 comments on commit 1d1f166

Please sign in to comment.