Skip to content

Commit ee3302b

Browse files
committed
Merge branch '3.4' into 4.4
* 3.4: ignore microseconds submitted by Edge
2 parents 14c6ba2 + b8ec858 commit ee3302b

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

Extension/Core/Type/TimeType.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6060
}
6161

6262
if ('single_text' === $options['widget']) {
63-
// handle seconds ignored by user's browser when with_seconds enabled
64-
// https://codereview.chromium.org/450533009/
65-
if ($options['with_seconds']) {
66-
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
67-
$data = $e->getData();
68-
if ($data && preg_match('/^\d{2}:\d{2}$/', $data)) {
69-
$e->setData($data.':00');
63+
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
64+
65+
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) use ($options) {
66+
$data = $e->getData();
67+
if ($data && preg_match('/^(?P<hours>\d{2}):(?P<minutes>\d{2})(?::(?P<seconds>\d{2})(?:\.\d+)?)?$/', $data, $matches)) {
68+
if ($options['with_seconds']) {
69+
// handle seconds ignored by user's browser when with_seconds enabled
70+
// https://codereview.chromium.org/450533009/
71+
$e->setData(sprintf('%s:%s:%s', $matches['hours'], $matches['minutes'], isset($matches['seconds']) ? $matches['seconds'] : '00'));
72+
} else {
73+
$e->setData(sprintf('%s:%s', $matches['hours'], $matches['minutes']));
7074
}
71-
});
72-
}
75+
}
76+
});
7377

7478
if (null !== $options['reference_date']) {
7579
$format = 'Y-m-d '.$format;
@@ -82,8 +86,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8286
}
8387
});
8488
}
85-
86-
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
8789
} else {
8890
$hourOptions = $minuteOptions = $secondOptions = [
8991
'error_bubbling' => true,

Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,54 @@ public function testSubmitDifferentTimezonesDuringDaylightSavingTimeUsingSingleT
328328
$this->assertSame('14:09:10', $form->getData()->format('H:i:s'));
329329
}
330330

331+
public function testSubmitWithoutSecondsAndBrowserAddingSeconds()
332+
{
333+
$form = $this->factory->create(static::TESTED_TYPE, null, [
334+
'model_timezone' => 'UTC',
335+
'view_timezone' => 'UTC',
336+
'input' => 'string',
337+
'widget' => 'single_text',
338+
'with_seconds' => false,
339+
]);
340+
341+
$form->submit('03:04:00');
342+
343+
$this->assertEquals('03:04:00', $form->getData());
344+
$this->assertEquals('03:04', $form->getViewData());
345+
}
346+
347+
public function testSubmitWithSecondsAndBrowserAddingMicroseconds()
348+
{
349+
$form = $this->factory->create(static::TESTED_TYPE, null, [
350+
'model_timezone' => 'UTC',
351+
'view_timezone' => 'UTC',
352+
'input' => 'string',
353+
'widget' => 'single_text',
354+
'with_seconds' => true,
355+
]);
356+
357+
$form->submit('03:04:00.000');
358+
359+
$this->assertEquals('03:04:00', $form->getData());
360+
$this->assertEquals('03:04:00', $form->getViewData());
361+
}
362+
363+
public function testSubmitWithoutSecondsAndBrowserAddingMicroseconds()
364+
{
365+
$form = $this->factory->create(static::TESTED_TYPE, null, [
366+
'model_timezone' => 'UTC',
367+
'view_timezone' => 'UTC',
368+
'input' => 'string',
369+
'widget' => 'single_text',
370+
'with_seconds' => false,
371+
]);
372+
373+
$form->submit('03:04:00.000');
374+
375+
$this->assertEquals('03:04:00', $form->getData());
376+
$this->assertEquals('03:04', $form->getViewData());
377+
}
378+
331379
public function testSetDataWithoutMinutes()
332380
{
333381
$form = $this->factory->create(static::TESTED_TYPE, null, [

0 commit comments

Comments
 (0)