Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Intl] Fix locale validator when canonicalize is true #60888

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
Jun 26, 2025

Conversation

rdavaillaud
Copy link
Contributor

@rdavaillaud rdavaillaud commented Jun 24, 2025

Q A
Branch? 6.4,7.x
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

When canonicalize is set to true, and the value passed to the validator is not a valid locale, ext-intl Locale::canonicalize should return null.

This is not the case with ICU <76, and this method returns an empty string.
But with the latest ICU lib, this method really returns null.

The problem is with Locales::exists() which only accept non null string.

This commit handles the returned null value in the validator.

@stof
Copy link
Member

stof commented Jun 24, 2025

Actually, could you add a test covering this to prevent regressions (i.e. a test validation a string for which the canonicalization returns null with modern ICU) ?

@rdavaillaud
Copy link
Contributor Author

Actually, could you add a test covering this to prevent regressions (i.e. a test validation a string for which the canonicalization returns null with modern ICU) ?

This should be handled by the LocaleValidatorTest of Validator component.
How to force a specific version of system library in the tests?

@stof
Copy link
Member

stof commented Jun 24, 2025

How to force a specific version of system library in the tests?

no need to try to force a system library (which won't be possible from PHP anyway). And no need to skip the test if ICU is older (the test will still pass if canonicalization returns an empty string).

@rdavaillaud
Copy link
Contributor Author

rdavaillaud commented Jun 24, 2025

no need to try to force a system library (which won't be possible from PHP anyway). And no need to skip the test if ICU is older (the test will still pass if canonicalization returns an empty string).

I don't understand what I have to do then.
The test is already in \Symfony\Component\Validator\Tests\Constraints\LocaleValidatorTest::testInvalidLocales()

@stof
Copy link
Member

stof commented Jun 24, 2025

Is the existing test failing when using ICU 76 (without your patch) ?

@rdavaillaud
Copy link
Contributor Author

@stof In fact, the problem is a little different than expected.

  • canonicalize returns null when the length of the value exceeds INTL_MAX_LOCALE_LEN. (as stated in PHP Documentation)
  • With ICU 76, canonicalize returns null when the length exceeds 11, at least on my system (docker, debian trixie, php 8.3) but don't know where the limit is from

And, the actual validator test don't cover those 2 cases.

I will make another PR, with clearer definition and tests.

@stof
Copy link
Member

stof commented Jun 24, 2025

@rdavaillaud no need to make another PR. You could add a test in this PR using a long locale name.

When canonicalize is set to true, and the value length exceeds INTL_MAX_LOCALE_LEN the validator throws an exception.

The Intl Locale::canonicalize() method returns null when the value is too long and Locales::exists() only accept non null string.

This commit allows to handle the null value as it should.

[Intl] windows / php 8.1 INTL_MAX_LOCALE_LEN
@nicolas-grekas
Copy link
Member

Thank you @rdavaillaud.

@nicolas-grekas nicolas-grekas merged commit c6f3da1 into symfony:6.4 Jun 26, 2025
8 of 11 checks passed
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