Skip to content

Commit 3aa9788

Browse files
committed
[Form] apply automatically step=1 for datetime-local input
1 parent 5a03896 commit 3aa9788

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Extension/Core/Type/DateTimeType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ public function buildView(FormView $view, FormInterface $form, array $options)
203203
// * the html5 is set to true
204204
if ($options['html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
205205
$view->vars['type'] = 'datetime-local';
206+
207+
// we need to force the browser to display the seconds by
208+
// adding the HTML attribute step if not already defined.
209+
// Otherwise the browser will not display and so not send the seconds
210+
// therefore the value will always be considered as invalid.
211+
if ($options['with_seconds'] && !isset($view->vars['attr']['step'])) {
212+
$view->vars['attr']['step'] = 1;
213+
}
206214
}
207215
}
208216

Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,37 @@ public function testDontPassHtml5TypeIfNotSingleText()
444444
$this->assertArrayNotHasKey('type', $view->vars);
445445
}
446446

447+
public function testSingleTextWidgetWithSecondsShouldHaveRightStepAttribute()
448+
{
449+
$view = $this->factory
450+
->create(static::TESTED_TYPE, null, [
451+
'widget' => 'single_text',
452+
'with_seconds' => true,
453+
])
454+
->createView()
455+
;
456+
457+
$this->assertArrayHasKey('step', $view->vars['attr']);
458+
$this->assertEquals(1, $view->vars['attr']['step']);
459+
}
460+
461+
public function testSingleTextWidgetWithSecondsShouldNotOverrideStepAttribute()
462+
{
463+
$view = $this->factory
464+
->create(static::TESTED_TYPE, null, [
465+
'widget' => 'single_text',
466+
'with_seconds' => true,
467+
'attr' => [
468+
'step' => 30,
469+
],
470+
])
471+
->createView()
472+
;
473+
474+
$this->assertArrayHasKey('step', $view->vars['attr']);
475+
$this->assertEquals(30, $view->vars['attr']['step']);
476+
}
477+
447478
public function testDateTypeChoiceErrorsBubbleUp()
448479
{
449480
$error = new FormError('Invalid!');

0 commit comments

Comments
 (0)