Skip to content

Commit c8ca1f9

Browse files
committed
Merge branch '4.4' into 5.0
* 4.4: [HttpClient] Fix typo [Mime] Fix boundary header ignore microseconds submitted by Edge
2 parents 93add72 + ee3302b commit c8ca1f9

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
@@ -61,16 +61,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6161
}
6262

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

7579
if (null !== $options['reference_date']) {
7680
$format = 'Y-m-d '.$format;
@@ -83,8 +87,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8387
}
8488
});
8589
}
86-
87-
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
8890
} else {
8991
$hourOptions = $minuteOptions = $secondOptions = [
9092
'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)