Skip to content

[Validator] Format datetime values in comparison constraints with dateFormat option #39857

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 2 commits into from

Conversation

YaFou
Copy link
Contributor

@YaFou YaFou commented Jan 16, 2021

Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #36784
License MIT
Doc PR TODO

I have introduced a new option for comparison constraints called dateFormat. This option gives the ability to use a different format for datetime values in the following placeholders: {{ value }} and {{ compared_value }}.

Before:

/**
 * @Assert\GreaterThanOrEqual(value="2020-01-01")
 */

will output: This value should be greater than Jan 1, 2020, 12:00 AM.

After:

/**
 * @Assert\GreaterThanOrEqual(value="2020-01-01", dateFormat="Y-m-d")
 */

will output: This value should be greater than 2020-01-01.

@YaFou YaFou force-pushed the message-format-comparison-validator branch from f019cc3 to 64f3fc5 Compare January 16, 2021 13:57
@YaFou YaFou force-pushed the message-format-comparison-validator branch from 64f3fc5 to 9395d1d Compare January 17, 2021 13:54
@YaFou YaFou changed the title [Validator] Format datetime values in comparison constraints with "valuesFormat" option [Validator] Format datetime values in comparison constraints with "dateFormat" option Jan 17, 2021
@YaFou YaFou force-pushed the message-format-comparison-validator branch 3 times, most recently from 56fe8dd to 3873d69 Compare January 17, 2021 14:12
@jderusse jderusse added this to the 5.x milestone Jan 18, 2021
@fancyweb
Copy link
Contributor

fancyweb commented Apr 7, 2021

I have three concerns with the proposed solution:

  1. The date format will behave differently if \IntlDateFormatter is available or not. I mean you have to know about the internal code to choose the right format.
  2. There is no way to easily configure a date format for all constraints.
  3. I would like to have a way to add more custom configurable formats (eg: for [Validator] Support \DateInterval in comparison constraints #33401) and we are not going to pile arguments everywhere.

I tried to resolve this issue at least 3 times but never came out with something good enough to me. I think the difficulty is that the formatting logic is internal to ConstraintValidator, it's nowhere in contracts. Having a real formatting system in the component could do the job. It could make sense at the violation builder level, but we cannot change the setParameter() method. Maybe we can add setParameterToFormat(string $key, mixed $value, int $flags) and then have a system that format the value to a string? It would be easy to hook in to add "custom" formatters. We would still need to find a way to easily allow override of the formats in the constraints.

@ro0NL
Copy link
Contributor

ro0NL commented Apr 26, 2021

Having a real formatting system in the component could do the job.

https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Translation/Formatter/MessageFormatterInterface.php 😅

i'd be generally curious if we can move formatting to the translation level using ICU formats.

eg. to avoid duplicating the message, perhaps a special translation key solely for date values.

@JohJohan
Copy link
Contributor

Having a real formatting system in the component could do the job.

https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Translation/Formatter/MessageFormatterInterface.php 😅

i'd be generally curious if we can move formatting to the translation level using ICU formats.

eg. to avoid duplicating the message, perhaps a special translation key solely for date values.

That could be a solution the default value should be full date format as that is how it is now

@YaFou YaFou force-pushed the message-format-comparison-validator branch from 3873d69 to 62a7952 Compare June 26, 2021 08:27
@YaFou YaFou force-pushed the message-format-comparison-validator branch from 62a7952 to 478b8ec Compare June 26, 2021 08:28
@YaFou
Copy link
Contributor Author

YaFou commented Jun 26, 2021

The date format will behave differently if \IntlDateFormatter is available or not. I mean you have to know about the internal code to choose the right format.

I'm facing the issue where the tests do not provide the intl extension and tests break... I don't know what is the right way to fix it.

@OskarStark OskarStark changed the title [Validator] Format datetime values in comparison constraints with "dateFormat" option [Validator] Format datetime values in comparison constraints with dateFormat option Aug 4, 2021
@fabpot fabpot removed this from the 5.4 milestone Nov 16, 2021
@fabpot fabpot added this to the 6.1 milestone Nov 16, 2021
@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@louismariegaborit
Copy link
Contributor

I'm very interesting about this feature. What is missing for the PR will be merged ?

@fabpot
Copy link
Member

fabpot commented Jul 23, 2022

Closing for the reasons explained by @fancyweb

@fabpot fabpot closed this Jul 23, 2022
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.

[Validator] Support date format option to format invalid message
10 participants