8000 [Form] Fix Date\TimeType marked as invalid on request with single_text and zero seconds by LuisDeimos · Pull Request #20307 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix Date\TimeType marked as invalid on request with single_text and zero seconds #20307

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

Closed
wants to merge 6 commits into from
Closed
Prev Previous commit
Next Next commit
[Form] fixed Date\TimeType marked as invalid on request with single_t…
…ext and zero seconds (LuisDeimos)
  • Loading branch information
LuisDeimos committed Oct 27, 2016
commit 006f2368f66c015b383bf069524b3f3910c21052
11 changes: 5 additions & 6 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ 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 and seconds is 00
// 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) {
if ($data = $e->getData()) {
if (preg_match('/^\d{2}:\d{2}$/', $data)) {
$e->setData($data.':00');
}
$data = $e->getData();
if ($data && preg_match('/^\d{2}:\d{2}$/', $data)) {
$e->setData($data.':00');
}

});
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ public function testSubmitStringSingleTextWithoutMinutes()
$this->assertEquals('03', $form->getViewData());
}

public function testSubmitWithSecondsAndBrowserOmissionSeconds()
{
$form = $this->factory->create('time', null, array(
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'input' => 'string',
'widget' => 'single_text',
'with_seconds' => true,
));

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

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

public function testSetDataWithoutMinutes()
{
$form = $this->factory->create('time', null, array(
Expand Down
0