[Form] Fixed DateType format option
This commit is contained in:
parent
ee69018bf4
commit
9e0d531d36
@ -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.');
|
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'))) {
|
if ('single_text' === $options['widget']) {
|
||||||
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('single_text' === $options['widget']) {
|
|
||||||
$builder->addViewTransformer(new DateTimeToLocalizedStringTransformer(
|
$builder->addViewTransformer(new DateTimeToLocalizedStringTransformer(
|
||||||
$options['model_timezone'],
|
$options['model_timezone'],
|
||||||
$options['view_timezone'],
|
$options['view_timezone'],
|
||||||
@ -65,6 +65,10 @@ class DateType extends AbstractType
|
|||||||
$pattern
|
$pattern
|
||||||
));
|
));
|
||||||
} else {
|
} 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(
|
$yearOptions = $monthOptions = $dayOptions = array(
|
||||||
'error_bubbling' => true,
|
'error_bubbling' => true,
|
||||||
);
|
);
|
||||||
|
@ -67,6 +67,22 @@ class DateTypeTest extends TestCase
|
|||||||
$this->assertEquals('2010-06-02', $form->getViewData());
|
$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()
|
public function testSubmitFromSingleTextDateTime()
|
||||||
{
|
{
|
||||||
// we test against "de_AT", so we need the full implementation
|
// 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
|
* @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()
|
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
|
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user