Skip to content
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

[Validator] Implement countUnit option for Length constraint #49464

Merged
merged 1 commit into from Feb 21, 2023
Merged

[Validator] Implement countUnit option for Length constraint #49464

merged 1 commit into from Feb 21, 2023

Conversation

spackmat
Copy link
Contributor

@spackmat spackmat commented Feb 21, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #49309
License MIT
Doc PR symfony/symfony-docs#17948

This implements the countUnit option for the Length constraint as proposed and discussed in #49309. That allows to control which strlen-method is used to count the string length for the Length constraint:

  • Length::COUNT_BYTES counts with \strlen()
  • Length::COUNT_CODEPOINTS counts with \mb_strlen() (default setting reflecting the former behavior)
  • Length::COUNT_GRAPHEMES counts with \grapheme_strlen() (e.g. emojis count as 1)

Also implements the corresponding tests.

Examples:

// count unicode characters like emojis by their bytes for your legacy database needs
new Length(max: 255, countUnit: Length::COUNT_BYTES);
// count unicode characters like emojis by their codepoints for your more modern database needs
new Length(max: 255); // that is the default like before or set explicitly to Length::COUNT_CODEPOINTS)
// count unicode graphemes (or ZWJ sequences) like emojis as 1 length for your fancy emoji-only microblogging service
new Length(max: 280, countUnit: Length::COUNT_GRAPHEMES);

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.

LGTM thanks, I just have minor comments.

@nicolas-grekas
Copy link
Member

Please rebase+squash to get rid of merge commits.

@nicolas-grekas
Copy link
Member

Thank you @spackmat.

@spackmat
Copy link
Contributor Author

Thanks @nicolas-grekas and @stof! I'll provide a PR for the docs.

@fabpot fabpot mentioned this pull request May 1, 2023
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] Make Length() Constraint configurable which strlen()-variant its Validator uses
4 participants