[Form] Automatically add step attribute to HTML5 time widgets to display seconds if needed

This commit is contained in:
Tugdual Saunier 2014-03-04 11:12:50 +00:00 committed by Fabien Potencier
parent 67be44737e
commit a379298db9
2 changed files with 35 additions and 0 deletions

View File

@ -140,6 +140,14 @@ class TimeType extends AbstractType
if ('single_text' === $options['widget']) {
$view->vars['type'] = 'time';
// we need to force the browser to display the seconds by
// adding the HTML attribute step if not already defined.
// Otherwise the browser will not display and so not send the seconds
// therefore the value will always be considered as invalid.
if ($options['with_seconds'] && !isset($view->vars['attr']['step'])) {
$view->vars['attr']['step'] = 1;
}
}
}

View File

@ -492,6 +492,33 @@ class TimeTypeTest extends TypeTestCase
$this->assertEquals('time', $view->vars['type']);
}
public function testSingleTextWidgetWithSecondsShouldHaveRightStepAttribute()
{
$form = $this->factory->create('time', null, array(
'widget' => 'single_text',
'with_seconds' => true,
));
$view = $form->createView();
$this->assertArrayHasKey('step', $view->vars['attr']);
$this->assertEquals(1, $view->vars['attr']['step']);
}
public function testSingleTextWidgetWithSecondsShouldNotOverrideStepAttribute()
{
$form = $this->factory->create('time', null, array(
'widget' => 'single_text',
'with_seconds' => true,
'attr' => array(
'step' => 30
)
));
$view = $form->createView();
$this->assertArrayHasKey('step', $view->vars['attr']);
$this->assertEquals(30, $view->vars['attr']['step']);
}
public function testPassDefaultEmptyValueToViewIfNotRequired()
{
$form = $this->factory->create('time', null, array(