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

This commit is contained in:
KUBO Atsuhiro 2012-05-25 14:24:04 +09:00
parent 167779e546
commit 7e3213cf3f
4 changed files with 33 additions and 1 deletions

View File

@ -157,6 +157,8 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
$calendar = $this->calendar;
$pattern = $this->pattern;
return new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
$intlDateFormatter->setLenient(false);
return $intlDateFormatter;
}
}

View File

@ -61,6 +61,7 @@ class DateType extends AbstractType
\IntlDateFormatter::GREGORIAN,
$pattern
);
$formatter->setLenient(false);
if ($options['widget'] === 'single_text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $format, \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $pattern));

View File

@ -286,4 +286,14 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
{
new DateTimeToLocalizedStringTransformer(null, null, null, 'foobar');
}
/**
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformWithNonExistingDate()
{
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::SHORT);
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('31.04.10 04:05'));
}
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Tests\Component\Form\Extension\Core\Type;
require_once __DIR__ . '/LocalizedTestCase.php';
use Symfony\Component\Form\FormError;
class DateTypeTest extends LocalizedTestCase
{
@ -460,4 +461,22 @@ class DateTypeTest extends LocalizedTestCase
$this->assertSame('single_text', $view->get('widget'));
}
public function testInvalidDateWithSingleTextDateTime()
{
$form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'single_text',
'input' => 'datetime',
'invalid_message' => 'Customized invalid message',
));
$form->bind('31.4.2012');
$this->assertFalse($form->isValid());
$this->assertNull($form->getData());
$this->assertEquals('31.4.2012', $form->getClientData());
$this->assertEquals(array(new FormError('Customized invalid message', array())), $form->getErrors());
}
}