Skip to content

[Validator] Add Week constraint #57908

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 13, 2024
Merged

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Aug 2, 2024

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

Validates weeks in the format 2024-W32. You can also pass min and max weeks to validate and range of weeks.

@xabbuh
Copy link
Member

xabbuh commented Aug 2, 2024

What I wonder: Do we really need the strict toggle? Are there use cases where we don't want to be that strict? IMO those use cases could be covered in userland using the Regex constraint.

@alexandre-daubois
Copy link
Member Author

ISO8601 only defines that week number must be between 1 and 53 it seems, and the week number is not strictly validated depending on the year.

To me, it is useful depending on if you only want to stick to ISO or if you want a little bit of additional check.

@javiereguiluz
Copy link
Member

I think I agree with @xabbuh here. In the equivalent <input type="week"/> field, you can pass invalid values if you set them manually or via JS (e.g. 2024-W53) but when using the browser UI, you can only select correct week numbers (See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/week#validation)

So, I think we should remove the strict option and make that strict behavior the default one.


An additional comment: should we add the max and min options to restrict which weeks are considered valid? The <input type="week"/> field defines that too. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/week#setting_maximum_and_minimum_weeks

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Aug 8, 2024

Thank you Javier for your super relevant feedback! I think @xabbuh and you are right about strict. I implemented min and max weeks

throw new UnexpectedValueException($value, 'string');
}

if (!preg_match('/^\d{4}-W(0[1-9]|[1-4][0-9]|5[0-3])$/', $value)) {
Copy link
Member

Choose a reason for hiding this comment

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

something we might want to do on all validators:

Suggested change
if (!preg_match('/^\d{4}-W(0[1-9]|[1-4][0-9]|5[0-3])$/', $value)) {
if (!preg_match('/^\d{4}-W(0[1-9]|[1-4][0-9]|5[0-3])$/D', $value)) {

Copy link
Member Author

@alexandre-daubois alexandre-daubois Aug 12, 2024

Choose a reason for hiding this comment

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

I wasn't aware of this modifier, thanks. I added it, I'll have a look at a follow-up PR to add it where it could be relevant. Would it be considered a bugfix or new feature? Not sure on this one

@alexandre-daubois alexandre-daubois force-pushed the week-constraint branch 3 times, most recently from 3fe0b10 to 9328a5c Compare August 12, 2024 16:34
@xabbuh
Copy link
Member

xabbuh commented Aug 13, 2024

Thank you Alexandre.

@xabbuh xabbuh merged commit f652396 into symfony:7.2 Aug 13, 2024
9 of 10 checks passed
nicolas-grekas added a commit that referenced this pull request Aug 13, 2024
…(alexandre-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] Add `D` regex modifier in relevant validators

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

After #57908 (comment). I'm not sure if this should be considered a bugfix or a feature.

Commits
-------

7d1032b [Validator] Add `D` regex modifier in relevant validators
symfony-splitter pushed a commit to symfony/validator that referenced this pull request Aug 13, 2024
…(alexandre-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] Add `D` regex modifier in relevant validators

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

After symfony/symfony#57908 (comment). I'm not sure if this should be considered a bugfix or a feature.

Commits
-------

7d1032bbea [Validator] Add `D` regex modifier in relevant validators
@fabpot fabpot mentioned this pull request Oct 27, 2024
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] WeekType validator
7 participants