[Form] Fixed DateType format option

This commit is contained in:
HeahDude 2016-12-27 15:46:49 +01:00
parent ee69018bf4
commit 9e0d531d36
2 changed files with 37 additions and 4 deletions

View File

@ -51,11 +51,11 @@ class DateType extends AbstractType
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 @@ class DateType extends AbstractType
$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,
);

View File

@ -67,6 +67,22 @@ class DateTypeTest extends TestCase
$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 @@ class DateTypeTest extends TestCase
/**
* @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 @@ class DateTypeTest extends TestCase
));
}
/**
* @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
*/