Skip to content

[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

Merged
merged 1 commit into from
Aug 22, 2024

Conversation

alexandre-daubois
Copy link
Member

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #53894
License MIT

Supersedes #53895 which seems abandoned for 6 months. I genuinely think this feature worth it especially when dealing with calendars with different first day of week.

@xabbuh
Copy link
Member

xabbuh commented Aug 13, 2024

@alexandre-daubois In #53894 I raised the question if we really need to be able to change the calendar. For the initial issue being able to control the locale was sufficient. Do you see other use cases where we have a need for being able to control the calendar?

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Aug 13, 2024

Yes. I'm not sure there's a need to switch between Gregorian and Julian calendars. But there's also other calendars that may be used like the Arabic Calendar for instance. I also think the test cases reflect (at least) why I would use this option the most: it is particularly useful when dealing with different first day of week, i.e. Sunday in the USA, Monday in Europe, etc.

I think that if you couple this with #57908, this definitely makes even more sense.

Overall, I think it brings more flexibility so anyone is up to use the calendar of their choice, without being limited by the locale. It doesn't bring that much complexity to the code. I think handling a locale instead of a calendar would require more boilerplate than there is now for less flexibility.

@nicolas-grekas
Copy link
Member

About locales, it looks like they can embed the calendar? e.g. ja_JP@calendar=japanese
I'm thus wondering if "calendar" isn't a subset of "locale"?
Sorry I know very little on the topic.

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Aug 19, 2024

When using a locale like ja_JP@calendar=japanese or zh_TW@calendar=roc, you must also use the IntlDateFormatter::TRADITIONAL constant when creating the instance of IntlDateFormatter. This means that if we support the locale option, we should also support the calendar option in DateType to pass something else than IntlDateFormatter::GREGORIAN (which is currently hardcoded).

At the end of the day, it requires on more option to support and less flexibility than being able to pass an IntlCalendar directly. I think that the current solution is more flexible, as well as easier to understand with less boilerplate.

@alexandre-daubois alexandre-daubois force-pushed the date-type-calendar-option branch from da570c0 to d855ab5 Compare August 19, 2024 09:54
Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with some minor CS comments

@alexandre-daubois alexandre-daubois force-pushed the date-type-calendar-option branch from d855ab5 to 75dc4db Compare August 20, 2024 14:33
'input' => 'array',
'calendar' => $calendar,
]);
$form->submit('2024-03-31 2024w14');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add failing tests when submitting 2024-03-31 2024w13 with this calendar and 2024-03-31 2024w14 with the calendar configured on line 1179?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, which case would you like to test exactly by doing this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what would happen then. Would the form accept such an invalid behaviour? In the best case the transformer would issue a TransformationFailedException and we could test for the form being invalid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a bit of trouble with the weeks. I reworked the tests instead to use another calendar instead of playing on the edge of weeks (which also didn't seem to be really supported by this formtype). What do you think of the new version?

@@ -320,6 +321,7 @@ 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]);
Copy link
Member

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', '...')

Copy link
Member Author

@alexandre-daubois alexandre-daubois Aug 21, 2024

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?

@alexandre-daubois alexandre-daubois force-pushed the date-type-calendar-option branch from 75dc4db to 855ed6d Compare August 21, 2024 07:06
'format' => 'y-MM-dd',
'html5' => false,
'input' => 'array',
'calendar' => \IntlCalendar::createInstance(locale: 'zh_TW@calendar=roc'),
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment. Is it clearer to you now?

@alexandre-daubois alexandre-daubois force-pushed the date-type-calendar-option branch from 855ed6d to fca95c5 Compare August 22, 2024 06:44
@xabbuh
Copy link
Member

xabbuh commented Aug 22, 2024

Thank you @alexandre-daubois.

@xabbuh xabbuh merged commit 1c8cd56 into symfony:7.2 Aug 22, 2024
7 of 10 checks passed
@alexandre-daubois alexandre-daubois deleted the date-type-calendar-option branch August 22, 2024 07:04
@fabpot fabpot mentioned this pull request Oct 27, 2024
@@ -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.');
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having a look. Thanks for raising this issue!

nicolas-grekas added a commit that referenced this pull request Nov 27, 2024
…e` (alexandre-daubois)

This PR was merged into the 7.2 branch.

Discussion
----------

[Form] Allow integer for the `calendar` option of `DateType`

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Underlying mechanisms support integers for the calendar, so integers should be allowed in the form type option.

Spotted by `@stof` in #57960 (comment)

Commits
-------

5e75bed [Form] Allow integer for the `calendar` option of `DateType`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Form] Add DateType IntlCalendar option.
7 participants