Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c8240a0

Browse filesBrowse files
committed
[Form] Add input_format option to DateType and DateTimeType
1 parent 98693be commit c8240a0
Copy full SHA for c8240a0

File tree

5 files changed

+39
-2
lines changed
Filter options

5 files changed

+39
-2
lines changed

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ CHANGELOG
3232
}
3333
}
3434
```
35+
* added new option `input_format` to `DateType` and `DateTimeType` to specify the date format when using the `string` input.
3536

3637
4.2.0
3738
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
+4-1Lines 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
@@ -317,6 +318,8 @@ public function configureOptions(OptionsResolver $resolver)
317318

318319
return '';
319320
});
321+
322+
$resolver->setAllowedTypes('input_format', 'string');
320323
}
321324

322325
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/DateType.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php
+15Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php
+15Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.