Skip to content

Commit

Permalink
bug #36020 [Form] ignore microseconds submitted by Edge (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Form] ignore microseconds submitted by Edge

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #35989
| License       | MIT
| Doc PR        |

Commits
-------

20971df ignore microseconds submitted by Edge
  • Loading branch information
nicolas-grekas committed Mar 13, 2020
2 parents 523f5c0 + 20971df commit a562ba2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -57,16 +57,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ('single_text' === $options['widget']) {
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));

// 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->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']));
}
});
}
}
});
} else {
$hourOptions = $minuteOptions = $secondOptions = [
'error_bubbling' => true,
Expand Down
Expand Up @@ -239,6 +239,54 @@ public function testSubmitWithSecondsAndBrowserOmissionSeconds()
$this->assertEquals('03:04:00', $form->getViewData());
}

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 a562ba2

Please sign in to comment.