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 11f65024690385fb069f6806caeaf8fab6b07cbf
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,6 @@ public function reverseTransform($value)
throw new TransformationFailedException('Expected a string.');
}

// handle seconds ignored by user's browser when seconds as single_text is 0
if ($this->parseFormat === 'H:i:s|') {
if (!preg_match('((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9]))', $value) &&
preg_match('((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9]))', $value)) {
$value = $value.':00';
}
}

$outputTz = new \DateTimeZone($this->outputTimezone);
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);

Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\ReversedTransformer;
Expand Down Expand Up @@ -49,6 +51,19 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a link to some issue(s) here, ie the chromium one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can leave out and seconds is 00 (actually seconds are omitted which you already mentioned).

if ($options['with_seconds']) {
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
if ($data = $e->getData()) {
if (preg_match('/^\d{2}:\d{2}$/', $data)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be a single if()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    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');
            }
        });
    }`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented suggestions, has not simplified the condition 'if ($ options [' with_seconds '])' to avoid adding an not necessary listener

$e->setData($data.':00');
}
}

});
}

} else {
$hourOptions = $minuteOptions = $secondOptions = array(
'error_bubbling' => true,
Expand Down
0