Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 905be49

Browse filesBrowse files
committed
ignore microseconds submitted by Edge
1 parent bdd66aa commit 905be49
Copy full SHA for 905be49

File tree

2 files changed

+43
-9
lines changed
Filter options

2 files changed

+43
-9
lines changed

‎src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
+11-9Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5757
if ('single_text' === $options['widget']) {
5858
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
5959

60-
// handle seconds ignored by user's browser when with_seconds enabled
61-
// https://codereview.chromium.org/450533009/
62-
if ($options['with_seconds']) {
63-
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
64-
$data = $e->getData();
65-
if ($data && preg_match('/^\d{2}:\d{2}$/', $data)) {
66-
$e->setData($data.':00');
60+
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) use ($options) {
61+
$data = $e->getData();
62+
if ($data && preg_match('/^(?P<hours>\d{2}):(?P<minutes>\d{2})(?::(?P<seconds>\d{2})\.0{3})?$/', $data, $matches)) {
63+
if ($options['with_seconds']) {
64+
// handle seconds ignored by user's browser when with_seconds enabled
65+
// https://codereview.chromium.org/450533009/
66+
$e->setData(sprintf('%s:%s:%s', $matches['hours'], $matches['minutes'], isset($matches['seconds']) ? $matches['seconds'] : '00'));
67+
} else {
68+
$e->setData(sprintf('%s:%s', $matches['hours'], $matches['minutes']));
6769
}
68-
});
69-
}
70+
}
71+
});
7072
} else {
7173
$hourOptions = $minuteOptions = $secondOptions = [
7274
'error_bubbling' => true,

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,38 @@ public function testSubmitWithSecondsAndBrowserOmissionSeconds()
239239
$this->assertEquals('03:04:00', $form->getViewData());
240240
}
241241

242+
public function testSubmitWithSecondsAndBrowserAddingMicroseconds()
243+
{
244+
$form = $this->factory->create(static::TESTED_TYPE, null, [
245+
'model_timezone' => 'UTC',
246+
'view_timezone' => 'UTC',
247+
'input' => 'string',
248+
'widget' => 'single_text',
249+
'with_seconds' => true,
250+
]);
251+
252+
$form->submit('03:04:00.000');
253+
254+
$this->assertEquals('03:04:00', $form->getData());
255+
$this->assertEquals('03:04:00', $form->getViewData());
256+
}
257+
258+
public function testSubmitWithoutSecondsAndBrowserAddingMicroseconds()
259+
{
260+
$form = $this->factory->create(static::TESTED_TYPE, null, [
261+
'model_timezone' => 'UTC',
262+
'view_timezone' => 'UTC',
263+
'input' => 'string',
264+
'widget' => 'single_text',
265+
'with_seconds' => false,
266+
]);
267+
268+
$form->submit('03:04:00.000');
269+
270+
$this->assertEquals('03:04:00', $form->getData());
271+
$this->assertEquals('03:04', $form->getViewData());
272+
}
273+
242274
public function testSetDataWithoutMinutes()
243275
{
244276
$form = $this->factory->create(static::TESTED_TYPE, null, [

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.