Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4b1296

Browse files
committed
[Form] Add input_format option to DateType and DateTimeType
1 parent db6784b commit b4b1296

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added `block_prefix` option to `BaseType`.
8+
* added new option `input_format` to `DateType` and `DateTimeType` to specify the date format when using the `string` input.
89

910
4.2.0
1011
-----

src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
176176
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
177177
} elseif ('string' === $options['input']) {
178178
$builder->addModelTransformer(new ReversedTransformer(
179-
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'])
179+
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $options['input_format'])
180180
));
181181
} elseif ('timestamp' === $options['input']) {
182182
$builder->addModelTransformer(new ReversedTransformer(
@@ -251,6 +251,7 @@ public function configureOptions(OptionsResolver $resolver)
251251
'empty_data' => function (Options $options) {
252252
return $options['compound'] ? [] : '';
253253
},
254+
'input_format' => 'Y-m-d H:i:s',
254255
]);
255256

256257
// Don't add some defaults in order to preserve the defaults
@@ -292,6 +293,8 @@ public function configureOptions(OptionsResolver $resolver)
292293
'text',
293294
'choice',
294295
]);
296+
297+
$resolver->setAllowedTypes('input_format', 'string');
295298
}
296299

297300
/**

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class_exists('IntlTimeZone', false) ? \IntlTimeZone::createDefault() : null,
154154
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
155155
} elseif ('string' === $options['input']) {
156156
$builder->addModelTransformer(new ReversedTransformer(
157-
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], 'Y-m-d')
157+
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $options['input_format'])
158158
));
159159
} elseif ('timestamp' === $options['input']) {
160160
$builder->addModelTransformer(new ReversedTransformer(
@@ -283,6 +283,7 @@ public function configureOptions(OptionsResolver $resolver)
283283
return $options['compound'] ? [] : '';
284284
},
285285
'choice_translation_domain' => false,
286+
'input_format' => 'Y-m-d',
286287
]);
287288

288289
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
@@ -305,6 +306,8 @@ public function configureOptions(OptionsResolver $resolver)
305306
$resolver->setAllowedTypes('years', 'array');
306307
$resolver->setAllowedTypes('months', 'array');
307308
$resolver->setAllowedTypes('days', 'array');
309+
310+
$resolver->setAllowedTypes('input_format', 'string');
308311
}
309312

310313
/**

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,19 @@ public function provideEmptyData()
671671
'Compound choice field' => ['choice', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
672672
];
673673
}
674+
675+
public function testSubmitStringWithCustomInputFormat(): void
676+
{
677+
$form = $this->factory->create(static::TESTED_TYPE, null, [
678+
'model_timezone' => 'UTC',
679+
'view_timezone' => 'UTC',
680+
'input' => 'string',
681+
'widget' => 'single_text',
682+
'input_format' => 'd/m/Y H:i:s P',
683+
]);
684+
685+
$form->submit('2018-01-14T21:29:00');
686+
687+
$this->assertSame('14/01/2018 21:29:00 +00:00', $form->getData());
688+
}
674689
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,4 +1037,19 @@ public function provideEmptyData()
10371037
'Compound choice fields' => ['choice', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
10381038
];
10391039
}
1040+
1041+
public function testSubmitStringWithCustomInputFormat(): void
1042+
{
1043+
$form = $this->factory->create(static::TESTED_TYPE, null, [
1044+
'model_timezone' => 'UTC',
1045+
'view_timezone' => 'UTC',
1046+
'widget' => 'single_text',
1047+
'input' => 'string',
1048+
'input_format' => 'd/m/Y',
1049+
]);
1050+
1051+
$form->submit('2018-01-14');
1052+
1053+
$this->assertSame('14/01/2018', $form->getData());
1054+
}
10401055
}

0 commit comments

Comments
 (0)