[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:
parent
7e3213cf3f
commit
20b556dacb
@ -96,6 +96,10 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$dateTime = new \DateTime($value, new \DateTimeZone($this->outputTimezone));
|
$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
|
// Force value to be in same format as given to transform
|
||||||
if ($value !== $dateTime->format($this->format)) {
|
if ($value !== $dateTime->format($this->format)) {
|
||||||
|
@ -140,4 +140,13 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
|
|||||||
|
|
||||||
$reverseTransformer->reverseTransform('2010-2010-2010');
|
$reverseTransformer->reverseTransform('2010-2010-2010');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testReverseTransformWithNonExistingDate()
|
||||||
|
{
|
||||||
|
$reverseTransformer = new DateTimeToStringTransformer();
|
||||||
|
|
||||||
|
$this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
|
||||||
|
|
||||||
|
$reverseTransformer->reverseTransform('2010-04-31');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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['date']->getErrors());
|
||||||
$this->assertEquals(array(new FormError('Customized invalid message', array())), $form['time']->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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user