Skip to content

Commit a379298

Browse files
tucksaunfabpot
authored andcommitted
[Form] Automatically add step attribute to HTML5 time widgets to display seconds if needed
1 parent 67be447 commit a379298

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ public function buildView(FormView $view, FormInterface $form, array $options)
140140

141141
if ('single_text' === $options['widget']) {
142142
$view->vars['type'] = 'time';
143+
144+
// we need to force the browser to display the seconds by
145+
// adding the HTML attribute step if not already defined.
146+
// Otherwise the browser will not display and so not send the seconds
147+
// therefore the value will always be considered as invalid.
148+
if ($options['with_seconds'] && !isset($view->vars['attr']['step'])) {
149+
$view->vars['attr']['step'] = 1;
150+
}
143151
}
144152
}
145153

src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,33 @@ public function testSingleTextWidgetShouldUseTheRightInputType()
492492
$this->assertEquals('time', $view->vars['type']);
493493
}
494494

495+
public function testSingleTextWidgetWithSecondsShouldHaveRightStepAttribute()
496+
{
497+
$form = $this->factory->create('time', null, array(
498+
'widget' => 'single_text',
499+
'with_seconds' => true,
500+
));
501+
502+
$view = $form->createView();
503+
$this->assertArrayHasKey('step', $view->vars['attr']);
504+
$this->assertEquals(1, $view->vars['attr']['step']);
505+
}
506+
507+
public function testSingleTextWidgetWithSecondsShouldNotOverrideStepAttribute()
508+
{
509+
$form = $this->factory->create('time', null, array(
510+
'widget' => 'single_text',
511+
'with_seconds' => true,
512+
'attr' => array(
513+
'step' => 30
514+
)
515+
));
516+
517+
$view = $form->createView();
518+
$this->assertArrayHasKey('step', $view->vars['attr']);
519+
$this->assertEquals(30, $view->vars['attr']['step']);
520+
}
521+
495522
public function testPassDefaultEmptyValueToViewIfNotRequired()
496523
{
497524
$form = $this->factory->create('time', null, array(

0 commit comments

Comments
 (0)