-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add support for the calendar
option in DateType
#57960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void | |
{ | ||
$dateFormat = \is_int($options['format']) ? $options['format'] : self::DEFAULT_FORMAT; | ||
$timeFormat = \IntlDateFormatter::NONE; | ||
$calendar = \IntlDateFormatter::GREGORIAN; | ||
$calendar = $options['calendar'] ?? \IntlDateFormatter::GREGORIAN; | ||
$pattern = \is_string($options['format']) ? $options['format'] : ''; | ||
|
||
if (!\in_array($dateFormat, self::ACCEPTED_FORMATS, true)) { | ||
|
@@ -281,6 +281,7 @@ public function configureOptions(OptionsResolver $resolver): void | |
'format' => $format, | ||
'model_timezone' => null, | ||
'view_timezone' => null, | ||
'calendar' => null, | ||
'placeholder' => $placeholderDefault, | ||
'html5' => true, | ||
// Don't modify \DateTime classes by reference, we treat | ||
|
@@ -320,6 +321,9 @@ public function configureOptions(OptionsResolver $resolver): void | |
$resolver->setAllowedTypes('months', 'array'); | ||
$resolver->setAllowedTypes('days', 'array'); | ||
$resolver->setAllowedTypes('input_format', 'string'); | ||
$resolver->setAllowedTypes('calendar', ['null', \IntlCalendar::class]); | ||
|
||
$resolver->setInfo('calendar', 'The calendar to use for formatting and parsing the date. The value should be one of the \IntlDateFormatter calendar constants or an instance of the \IntlCalendar to use.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This information looks wrong. The allowed types don't allow the integer constants There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm having a look. Thanks for raising this issue! |
||
|
||
$resolver->setNormalizer('html5', static function (Options $options, $html5) { | ||
if ($html5 && 'single_text' === $options['widget'] && self::HTML5_FORMAT !== $options['format']) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1156,6 +1156,40 @@ public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone() | |
]); | ||
} | ||
|
||
public function testSubmitWithCustomCalendarOption() | ||
{ | ||
// Creates a new form using the "roc" (Republic Of China) calendar. This calendar starts in 1912, the year 2024 in | ||
// the Gregorian calendar is the year 113 in the "roc" calendar. | ||
$form = $this->factory->create(static::TESTED_TYPE, options: [ | ||
'format' => 'y-MM-dd', | ||
'html5' => false, | ||
'input' => 'array', | ||
'calendar' => \IntlCalendar::createInstance(locale: 'zh_TW@calendar=roc'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO the use of this calendar deserves some kind of comment. I believe it's not so obvious when reading the test why we submit a year like 113 but expect 2024 as the final result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a comment. Is it clearer to you now? |
||
]); | ||
$form->submit('113-03-31'); | ||
|
||
$this->assertSame('2024', $form->getData()['year'], 'The year should be converted to the default locale (en)'); | ||
$this->assertSame('31', $form->getData()['day']); | ||
$this->assertSame('3', $form->getData()['month']); | ||
|
||
$this->assertSame('113-03-31', $form->getViewData()); | ||
} | ||
|
||
public function testSetDataWithCustomCalendarOption() | ||
{ | ||
// Creates a new form using the "roc" (Republic Of China) calendar. This calendar starts in 1912, the year 2024 in | ||
// the Gregorian calendar is the year 113 in the "roc" calendar. | ||
$form = $this->factory->create(static::TESTED_TYPE, options: [ | ||
'format' => 'y-MM-dd', | ||
'html5' => false, | ||
'input' => 'array', | ||
'calendar' => \IntlCalendar::createInstance(locale: 'zh_TW@calendar=roc'), | ||
]); | ||
$form->setData(['year' => '2024', 'month' => '3', 'day' => '31']); | ||
|
||
$this->assertSame('113-03-31', $form->getViewData()); | ||
} | ||
|
||
protected function getTestOptions(): array | ||
{ | ||
return ['widget' => 'choice']; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add some brief info about this new option?
$resolver->setInfo('calendar', '...')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added! Is the message good to you?