Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down