Skip to content

[Form] Add calendar DateType option #53895

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

Closed
wants to merge 3 commits into from

Conversation

jan-pintr
Copy link

@jan-pintr jan-pintr commented Feb 10, 2024

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

Recently I required to render DateType in format with week of year [1] (week 13 of year 2024: 2024-03-31 2024w13) with independent IntlCalendar specification than provided from currently set Locale.

For example when project (default) locale is en_US (en). But you need to output in "European's format" it is not possible with current implementation. You would get 2024-03-31 2024w14, because first day of week configuration is taken from currently set locale.

This pull request adds calendar option to DateType.

$calendar = \IntlCalendar::createInstance();
$calendar->setFirstDayOfWeek(\IntlCalendar::DOW_MONDAY);
$calendar->setMinimalDaysInFirstWeek(4);

$form->add('with_week', DateType::class, [
    'format' => "y-MM-dd y'w'w",
    'calendar' => $calendar,          
]);

[1] https://unicode.org/reports/tr35/tr35-dates.html#Date_Patterns_Week_Of_Year

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

$form->submit('2024-03-31 2024w13');

$this->assertEquals($output, $form->getData());
$this->assertEquals('2024-03-31 2024w13', $form->getViewData());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use asserSame ?

Same for the other tests

Copy link
Author

Choose a reason for hiding this comment

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

I have the change ready on local, but I'm not a big fan of it as other tests in changed files use assertEquals. Could somebody else give us an opinion?

Copy link
Member

Choose a reason for hiding this comment

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

opinion: never rely on assertEquals, same as never rely on == instead of ===

Copy link
Contributor

Choose a reason for hiding this comment

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

You can change it to assertSame across the whole file 👍

@carsonbot carsonbot changed the title Add calendar DateType option [Form] Add calendar DateType option Feb 14, 2024
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Here are random comments :)

@@ -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 = null !== $options['calendar'] ? $options['calendar'] : \IntlDateFormatter::GREGORIAN;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$calendar = null !== $options['calendar'] ? $options['calendar'] : \IntlDateFormatter::GREGORIAN;
$calendar = $options['calendar'] ?? \IntlDateFormatter::GREGORIAN;

$pattern,
);

$this->assertEquals('2024-03-31 2024w13', $transformer->transform($dateTime));
Copy link
Member

Choose a reason for hiding this comment

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

assertSame everywhere please

null,
$calendar,
$pattern,
);
Copy link
Member

Choose a reason for hiding this comment

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

we prefer long lines, maybe using named args could help? our put this on one line otherwise


$pattern = "y-MM-dd y'w'w";

$transformer = new DateTimeToLocalizedStringTransformer(
Copy link
Member

Choose a reason for hiding this comment

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

oneliners FTW

$calendar->setFirstDayOfWeek(\IntlCalendar::DOW_MONDAY);
$calendar->setMinimalDaysInFirstWeek(4);

$transformer = new DateTimeToLocalizedStringTransformer(
Copy link
Member

Choose a reason for hiding this comment

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

oneliner

@nicolas-grekas
Copy link
Member

@jan-pintr are you interested in finishing this PR?

@xabbuh
Copy link
Member

xabbuh commented Aug 9, 2024

closing in favour of #57960, thank you for starting the work @jan-pintr

@xabbuh xabbuh closed this Aug 9, 2024
xabbuh added a commit that referenced this pull request Aug 22, 2024
…ype` (alexandre-daubois)

This PR was merged into the 7.2 branch.

Discussion
----------

[Form] Add support for the `calendar` option in `DateType`

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

Commits
-------

fca95c5 [Form] Add support for the `calendar` option in `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.
6 participants