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

Skip to content
8000

Commit 4b71d52

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

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ public function reverseTransform($value)
132132
throw new TransformationFailedException('Expected a string.');
133133
}
134134

135-
// handle seconds ignored by user's browser when seconds as single_text is 0
136-
if ($this->parseFormat === 'H:i:s|') {
137-
if (!preg_match('((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9]))', $value) &&
138-
preg_match('((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9]))', $value)) {
139-
$value = $value.':00';
140-
}
141-
}
142-
143135
$outputTz = new \DateTimeZone($this->outputTimezone);
144136
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
145137

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\FormEvent;
16+
use Symfony\Component\Form\FormEvents;
1517
use Symfony\Component\Form\FormInterface;
1618
use Symfony\Component\Form\FormBuilderInterface;
1719
use Symfony\Component\Form\ReversedTransformer;
@@ -49,6 +51,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4951

5052
if ('single_text' === $options['widget']) {
5153
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
54+
55+
// handle seconds ignored by user's browser when with_seconds enabled and seconds is 00
56+
if ($options['with_seconds']) {
57+
$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+
}
62+
}
63+
64+
});
65+
}
66+
5267
} else {
5368
$hourOptions = $minuteOptions = $secondOptions = array(
5469
'error_bubbling' => true,

0 commit comments

Comments
 (0)
0