Skip to content

Commit

Permalink
Merge branch '3.4' into 4.4
Browse files Browse the repository at this point in the history
* 3.4:
  ignore microseconds submitted by Edge
  • Loading branch information
fabpot committed Mar 15, 2020
2 parents 14c6ba2 + b8ec858 commit ee3302b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

if ('single_text' === $options['widget']) {
// handle seconds ignored by user's browser when with_seconds enabled
// https://codereview.chromium.org/450533009/
if ($options['with_seconds']) {
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
$data = $e->getData();
if ($data && preg_match('/^\d{2}:\d{2}$/', $data)) {
$e->setData($data.':00');
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) use ($options) {
$data = $e->getData();
if ($data && preg_match('/^(?P<hours>\d{2}):(?P<minutes>\d{2})(?::(?P<seconds>\d{2})(?:\.\d+)?)?$/', $data, $matches)) {
if ($options['with_seconds']) {
// handle seconds ignored by user's browser when with_seconds enabled
// https://codereview.chromium.org/450533009/
$e->setData(sprintf('%s:%s:%s', $matches['hours'], $matches['minutes'], isset($matches['seconds']) ? $matches['seconds'] : '00'));
} else {
$e->setData(sprintf('%s:%s', $matches['hours'], $matches['minutes']));
}
});
}
}
});

if (null !== $options['reference_date']) {
$format = 'Y-m-d '.$format;
Expand All @@ -82,8 +86,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
});
}

$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
} else {
$hourOptions = $minuteOptions = $secondOptions = [
'error_bubbling' => true,
Expand Down
48 changes: 48 additions & 0 deletions Tests/Extension/Core/Type/TimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,54 @@ public function testSubmitDifferentTimezonesDuringDaylightSavingTimeUsingSingleT
$this->assertSame('14:09:10', $form->getData()->format('H:i:s'));
}

public function testSubmitWithoutSecondsAndBrowserAddingSeconds()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'input' => 'string',
'widget' => 'single_text',
'with_seconds' => false,
]);

$form->submit('03:04:00');

$this->assertEquals('03:04:00', $form->getData());
$this->assertEquals('03:04', $form->getViewData());
}

public function testSubmitWithSecondsAndBrowserAddingMicroseconds()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'input' => 'string',
'widget' => 'single_text',
'with_seconds' => true,
]);

$form->submit('03:04:00.000');

$this->assertEquals('03:04:00', $form->getData());
$this->assertEquals('03:04:00', $form->getViewData());
}

public function testSubmitWithoutSecondsAndBrowserAddingMicroseconds()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'input' => 'string',
'widget' => 'single_text',
'with_seconds' => false,
]);

$form->submit('03:04:00.000');

$this->assertEquals('03:04:00', $form->getData());
$this->assertEquals('03:04', $form->getViewData());
}

public function testSetDataWithoutMinutes()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
Expand Down

0 comments on commit ee3302b

Please sign in to comment.