8000 [Validator][Date] Introduce format for date constraint by chadyred · Pull Request #58602 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator][Date] Introduce format for date constraint #58602

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

Conversation

chadyred
Copy link
Contributor
@chadyred chadyred commented Oct 18, 2024
Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
License MIT

Hi !

This is a PR to add the format for the constraint to validate a Date because it is too static for a validation and specially if we want to match a particular format of date which exist in many library ( like d/m/Y or m.d.Y ).

It is a proposal made to be able to match a given format of Date for a given value. As in PHP only DateTime(Immutable) is able to use format with all type of pattern, it seems natural to me to use it here.

Don't hesitate if you have any question.

@chadyred chadyred changed the title Feat/format on date constraint [Validator][Date] Introduce format for date constraint Oct 21, 2024
@chadyred chadyred force-pushed the feat/format-on-date-constraint branch from 0d114cc to f519cc7 Compare October 21, 2024 07:48
@chadyred
Copy link
Contributor Author

Hi, CI seems in pain for 2 jobs in 2 different commit, something like :

 Cloning into '/home/runner/work/symfony/symfony/src/Symfony/Component/Workflow/vendor/symfony/validator'...  
    error: RPC failed; HTTP 504 curl 22 The requested URL returned error: 504                                    
    fatal: expected 'packfile'        

@chadyred chadyred force-pushed the feat/format-on-date-constraint branch from f519cc7 to d83b2a0 Compare October 21, 2024 12:03
@chadyred
Copy link
Contributor Author

CI seems to be good now

@nicolas-grekas
Copy link
Member

You can achieve this using the DateTime constraint if I'm not wrong. Doing this change is basically making Date and DateTime constraints very similar.

@chadyred
Copy link
Contributor Author
8000

You are right @nicolas-grekas , so, the need it is to map Date from input of any format and apply the constraint on many format.

Date constraint is freeze with Y-m-d, before this code I think about something more simple but really poor compare to the format of DateTime(Immutable)

   public const PATTERN_YMD = '/^Y(?<separator1>[.\-\/])m(?<separator2>[.\-\/])d$/D';
    public const PATTERN_MDY = '/^m(?<separator1>[.\-\/])d(?<separator2>[.\-\/])Y$/D';
    public const PATTERN_DMY = '/^d(?<separator1>[.\-\/])m(?<separator2>[.\-\/])Y$/D';

    public const ACCEPTED_DATE_FORMATS_REGEX = [
        'Y[.-/]m[.-/]d' => self::PATTERN_YMD,
        'm[.-/]d[.-/]Y' => self::PATTERN_MDY,
        'd[.-/]m[.-/]Y' => self::PATTERN_DMY,
    ];

I agree it could be redundant of DateTimeConstraint with the FORMAT

@nicolas-grekas
Copy link
Member

So do we need this PR when you can use DateTime instead?

@chadyred
Copy link
Contributor Author

So I close for redundancy

@chadyred chadyred closed this Oct 23, 2024
@chadyred
Copy link
Contributor Author
chadyred commented Oct 25, 2024

I rethink about that : @nicolas-grekas Date constraint could be deprecated, but, we can keep the PATTERN constant to ease the migration, by precising that application have to do #[DateTimeConstraint(format: DATE_PATTERN) instead (for attribute example) ? Because if it is a duplication, maybe we have the same possibility also to do #[DateTimeConstraint(format: 'Y-m-d') maybe is valid ? Their is already 2 ways to do the same thing 🤔

@nicolas-grekas
Copy link
Member

I see no hurry in deprecating. Let's save people from doing changes of little value.

@chadyred
Copy link
Contributor Author

All right, thanks for your light and advice !

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.

4 participants
0