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

[Form] ignore microseconds submitted by Edge #36020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
ignore microseconds submitted by Edge
  • Loading branch information
xabbuh committed Mar 13, 2020
commit 20971dff8297d59c8cd4bc5f2ba604a4c13913c8
20 changes: 11 additions & 9 deletions 20 src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.