8000 [Form] fixed Date\TimeType marked as invalid on request with single_… · symfony/symfony@006f236 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 006f236

Browse files
committed
[Form] fixed Date\TimeType marked as invalid on request with single_text and zero seconds (LuisDeimos)
1 parent 4adaa2a commit 006f236

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5252
if ('single_text' === $options['widget']) {
5353
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
5454

55-
// handle seconds ignored by user's browser when with_seconds enabled and seconds is 00
55+
// handle seconds ignored by user's browser when with_seconds enabled
56+
// https://codereview.chromium.org/450533009/
5657
if ($options['with_seconds']) {
5758
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
58-
if ($data = $e->getData()) {
59-
if (preg_match('/^\d{2}:\d{2}$/', $data)) {
60-
$e->setData($data.':00');
61-
}
59+
$data = $e->getData();
60+
if ($data && preg_match('/^\d{2}:\d{2}$/', $data)) {
61+
$e->setData($data.':00');
6262
}
63-
6463
});
6564
}
6665
} else {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ public function testSubmitStringSingleTextWithoutMinutes()
221221
$this->assertEquals('03', $form->getViewData());
222222
}
223223

224+
public function testSubmitWithSecondsAndBrowserOmissionSeconds()
225+
{
226+
$form = $this->factory->create('time', null, array(
227+
'model_timezone' => 'UTC',
228+
'view_timezone' => 'UTC',
229+
'input' => 'string',
230+
'widget' => 'single_text',
231+
'with_seconds' => true,
232+
));
233+
234+
$form->submit('03:04');
235+
236+
$this->assertEquals('03:04:00', $form->getData());
237+
$this->assertEquals('03:04:00', $form->getViewData());
238+
}
239+
224240
public function testSetDataWithoutMinutes()
225241
{
226242
$form = $this->factory->create('time', null, array(

0 commit comments

Comments
 (0)
0