[Form] fixed a bug that caused input date validation not to be strict when using the single_text widget with a datetime field

This commit is contained in:
KUBO Atsuhiro 2012-05-25 17:41:38 +09:00
parent 7e3213cf3f
commit 20b556dacb
3 changed files with 31 additions and 0 deletions

View File

@ -96,6 +96,10 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
try {
$dateTime = new \DateTime($value, new \DateTimeZone($this->outputTimezone));
$lastErrors = \DateTime::getLastErrors();
if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
throw new \UnexpectedValueException(implode(', ', array_merge(array_values($lastErrors['warnings']), array_values($lastErrors['errors']))));
}
// Force value to be in same format as given to transform
if ($value !== $dateTime->format($this->format)) {

View File

@ -140,4 +140,13 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
$reverseTransformer->reverseTransform('2010-2010-2010');
}
public function testReverseTransformWithNonExistingDate()
{
$reverseTransformer = new DateTimeToStringTransformer();
$this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
$reverseTransformer->reverseTransform('2010-04-31');
}
}

View File

@ -234,4 +234,22 @@ class DateTimeTypeTest extends LocalizedTestCase
$this->assertEquals(array(new FormError('Customized invalid message', array())), $form['date']->getErrors());
$this->assertEquals(array(new FormError('Customized invalid message', array())), $form['time']->getErrors());
}
public function testSubmit_invalidDateTimeSingleText()
{
$form = $this->factory->create('datetime', null, array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'input' => 'datetime',
'widget' => 'single_text',
'invalid_message' => 'Customized invalid message',
));
$form->bind('2012-04-31 03:04:05');
$this->assertFalse($form->isValid());
$this->assertNull($form->getData());
$this->assertEquals('2012-04-31 03:04:05', $form->getClientData());
$this->assertEquals(array(new FormError('Customized invalid message', array())), $form->getErrors());
}
}