Skip to content

Commit

Permalink
[Form] fixed a bug that caused input date validation not to be strict…
Browse files Browse the repository at this point in the history
… when using the single_text widget with a date field
  • Loading branch information
iteman committed May 28, 2012
1 parent 167779e commit 7e3213c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
Expand Up @@ -157,6 +157,8 @@ protected function getIntlDateFormatter()
$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;
}
}
Expand Up @@ -61,6 +61,7 @@ public function buildForm(FormBuilder $builder, array $options)
\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));
Expand Down
Expand Up @@ -286,4 +286,14 @@ public function testValidateTimeFormatOption()
{
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'));
}
}
Expand Up @@ -13,6 +13,7 @@

require_once __DIR__ . '/LocalizedTestCase.php';

use Symfony\Component\Form\FormError;

class DateTypeTest extends LocalizedTestCase
{
Expand Down Expand Up @@ -460,4 +461,22 @@ public function testPassWidgetToView()

$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());
}
}

0 comments on commit 7e3213c

Please sign in to comment.