ignore microseconds submitted by Edge
This commit is contained in:
parent
523f5c04ab
commit
20971dff82
@ -57,16 +57,18 @@ class TimeType extends AbstractType
|
|||||||
if ('single_text' === $options['widget']) {
|
if ('single_text' === $options['widget']) {
|
||||||
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
|
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
|
||||||
|
|
||||||
// handle seconds ignored by user's browser when with_seconds enabled
|
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) use ($options) {
|
||||||
// https://codereview.chromium.org/450533009/
|
$data = $e->getData();
|
||||||
if ($options['with_seconds']) {
|
if ($data && preg_match('/^(?P<hours>\d{2}):(?P<minutes>\d{2})(?::(?P<seconds>\d{2})(?:\.\d+)?)?$/', $data, $matches)) {
|
||||||
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
|
if ($options['with_seconds']) {
|
||||||
$data = $e->getData();
|
// handle seconds ignored by user's browser when with_seconds enabled
|
||||||
if ($data && preg_match('/^\d{2}:\d{2}$/', $data)) {
|
// https://codereview.chromium.org/450533009/
|
||||||
$e->setData($data.':00');
|
$e->setData(sprintf('%s:%s:%s', $matches['hours'], $matches['minutes'], isset($matches['seconds']) ? $matches['seconds'] : '00'));
|
||||||
|
} else {
|
||||||
|
$e->setData(sprintf('%s:%s', $matches['hours'], $matches['minutes']));
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
} else {
|
} else {
|
||||||
$hourOptions = $minuteOptions = $secondOptions = [
|
$hourOptions = $minuteOptions = $secondOptions = [
|
||||||
'error_bubbling' => true,
|
'error_bubbling' => true,
|
||||||
|
@ -239,6 +239,54 @@ class TimeTypeTest extends BaseTypeTest
|
|||||||
$this->assertEquals('03:04:00', $form->getViewData());
|
$this->assertEquals('03:04:00', $form->getViewData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitWithoutSecondsAndBrowserAddingSeconds()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, [
|
||||||
|
'model_timezone' => 'UTC',
|
||||||
|
'view_timezone' => 'UTC',
|
||||||
|
'input' => 'string',
|
||||||
|
'widget' => 'single_text',
|
||||||
|
'with_seconds' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$form->submit('03:04:00');
|
||||||
|
|
||||||
|
$this->assertEquals('03:04:00', $form->getData());
|
||||||
|
$this->assertEquals('03:04', $form->getViewData());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubmitWithSecondsAndBrowserAddingMicroseconds()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, [
|
||||||
|
'model_timezone' => 'UTC',
|
||||||
|
'view_timezone' => 'UTC',
|
||||||
|
'input' => 'string',
|
||||||
|
'widget' => 'single_text',
|
||||||
|
'with_seconds' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$form->submit('03:04:00.000');
|
||||||
|
|
||||||
|
$this->assertEquals('03:04:00', $form->getData());
|
||||||
|
$this->assertEquals('03:04:00', $form->getViewData());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubmitWithoutSecondsAndBrowserAddingMicroseconds()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, [
|
||||||
|
'model_timezone' => 'UTC',
|
||||||
|
'view_timezone' => 'UTC',
|
||||||
|
'input' => 'string',
|
||||||
|
'widget' => 'single_text',
|
||||||
|
'with_seconds' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$form->submit('03:04:00.000');
|
||||||
|
|
||||||
|
$this->assertEquals('03:04:00', $form->getData());
|
||||||
|
$this->assertEquals('03:04', $form->getViewData());
|
||||||
|
}
|
||||||
|
|
||||||
public function testSetDataWithoutMinutes()
|
public function testSetDataWithoutMinutes()
|
||||||
{
|
{
|
||||||
$form = $this->factory->create(static::TESTED_TYPE, null, [
|
$form = $this->factory->create(static::TESTED_TYPE, null, [
|
||||||
|
Reference in New Issue
Block a user