Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 2 commits into from

4 participants

@tucksaun
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
@tucksaun tucksaun [Form] Automatically add step attribute to HTML5 time widgets to disp…
…lay seconds if needed
6e41ee4
@tucksaun tucksaun added the Form label
@webmozart webmozart added the Bug label
@Tobion
Collaborator

:+1:

@tucksaun tucksaun [Form] add comments regarding why we add automatically the step attri…
…bute in TimeType
6f8ee8c
@fabpot
Owner

Thank you @tucksaun.

@fabpot fabpot referenced this pull request from a commit
@fabpot fabpot bug #10777 [Form] Automatically add step attribute to HTML5 time widg…
…ets 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
0762bae
@fabpot fabpot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 24, 2014
  1. @tucksaun

    [Form] Automatically add step attribute to HTML5 time widgets to disp…

    tucksaun authored
    …lay seconds if needed
Commits on May 16, 2014
  1. @tucksaun

    [Form] add comments regarding why we add automatically the step attri…

    tucksaun authored
    …bute in TimeType
This page is out of date. Refresh to see the latest.
View
8 src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
@@ -140,6 +140,14 @@ public function buildView(FormView $view, FormInterface $form, array $options)
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
27 src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php
@@ -492,6 +492,33 @@ public function testSingleTextWidgetShouldUseTheRightInputType()
$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(
Something went wrong with that request. Please try again.