8000 bug #10777 [Form] Automatically add step attribute to HTML5 time widg… · symfony/symfony@0762bae · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>

Commit 0762bae

Browse files
committed
bug #10777 [Form] Automatically add step attribute to HTML5 time widgets to display seconds if needed (tucksaun)
This PR was squashed before being merged into the 2.3 branch (closes #10777). Discussion ---------- [Form] Automatically add step attribute to HTML5 time widgets to display seconds if needed | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9976, #10203 | License | MIT | Doc PR | none Same issue as #9976 and #10203: when you add a `time` field to a form with options `single_text` (so HTML5) and `with_seconds`, the generated input does not contain the `step` attribute, therefore the browser does not show them, leading to an error at the submit because of an invalid format. Compared to #9976/#10203: * Unit testable * Available directly in the component * Available in other templating format than twig * Still able to customise the step attribute by hand Commits ------- a379298 [Form] Automatically add step attribute to HTML5 time widgets to display seconds if needed
2 parents 912a72f + a379298 commit 0762bae

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)
0