bug #20307 [Form] Fix Date\TimeType marked as invalid on request with single_text and zero seconds (LuisDeimos)
This PR was squashed before being merged into the 2.7 branch (closes #20307).
Discussion
----------
[Form] Fix Date\TimeType marked as invalid on request with single_text and zero seconds
| Q | A |
| --- | --- |
| Branch? | 2.7 |
| Bug fix? | yes |
| New feature? | no |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | yes |
| Fixed tickets | 20304 |
| License | MIT |
| Doc PR | |
Fix: When using a form with an Time type with option 'widget' => 'single_text', and 0 is selected in the seconds, we obtain an TransformationFailedException "Unable to reverse value for property path "[time]": Data missing". Check ticket #20304
Commits
-------
bcb03e0
[Form] Fix Date\TimeType marked as invalid on request with single_text and zero seconds
This commit is contained in:
commit
edf5d715e3
@ -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;
|
||||
@ -49,6 +51,17 @@ class TimeType extends AbstractType
|
||||
|
||||
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');
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
$hourOptions = $minuteOptions = $secondOptions = array(
|
||||
'error_bubbling' => true,
|
||||
|
@ -221,6 +221,22 @@ class TimeTypeTest extends TestCase
|
||||
$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(
|
||||
|
Reference in New Issue
Block a user