[Form] Fix Date\TimeType marked as invalid on request with single_text and zero seconds
This commit is contained in:
parent
8d1711a719
commit
bcb03e0a39
@ -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