be able to specify the input format for times

This commit is contained in:
Christian Flothmann 2019-02-23 11:36:32 +01:00
parent e9a2c3d753
commit 2d9bc18c1b
3 changed files with 21 additions and 2 deletions

View File

@ -38,7 +38,8 @@ CHANGELOG
}
}
```
* added new option `input_format` to `DateType` and `DateTimeType` to specify the date format when using the `string` input.
* added the `input_format` option to `DateType`, `DateTimeType`, and `TimeType` to specify the input format when setting
the `input` option to `string`
4.2.0
-----

View File

@ -164,7 +164,7 @@ class TimeType extends AbstractType
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], 'H:i:s')
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $options['input_format'])
));
} elseif ('timestamp' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
@ -257,6 +257,7 @@ class TimeType extends AbstractType
'seconds' => range(0, 59),
'widget' => 'choice',
'input' => 'datetime',
'input_format' => 'H:i:s',
'with_minutes' => true,
'with_seconds' => false,
'model_timezone' => null,
@ -298,6 +299,7 @@ class TimeType extends AbstractType
$resolver->setAllowedTypes('hours', 'array');
$resolver->setAllowedTypes('minutes', 'array');
$resolver->setAllowedTypes('seconds', 'array');
$resolver->setAllowedTypes('input_format', 'string');
}
/**

View File

@ -80,6 +80,22 @@ class TimeTypeTest extends BaseTypeTest
$this->assertEquals($input, $form->getViewData());
}
public function testSubmitStringWithCustomFormat()
{
$form = $this->factory->create(static::TESTED_TYPE, '11:33', [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
'input' => 'string',
'input_format' => 'H:i',
]);
$form->submit('03:24');
$this->assertEquals('03:24', $form->getData());
$this->assertEquals('03:24', $form->getViewData());
}
public function testSubmitTimestamp()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [