8000 feature #22353 [Validator] Add `canonicalize` option for `Locale` val… · symfony/symfony@5f0c279 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f0c279

Browse files
committed
feature #22353 [Validator] Add canonicalize option for Locale validator (phansys)
This PR was squashed before being merged into the 4.1-dev branch (closes #22353). Discussion ---------- [Validator] Add `canonicalize` option for `Locale` validator |Q |A | |--- |--- | |Branch |master| |Bug fix? |no | |New feature? |yes | |BC breaks? |no | |Deprecations?|no | |Tests pass? |yes | |Fixed tickets|n/a | |License |MIT | |Doc PR |n/a | Allow non canonicalized locales ('fr-FR' by instance) to pass the validation. Relates to symfony/symfony-docs#7660. Commits ------- 39dfa3d [Validator] Add option for LANG="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_CTYPE="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_ALL= validator
2 parents c557327 + 39dfa3d commit 5f0c279

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Symfony/Component/Validator/Constraints/Locale.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ class Locale extends Constraint
2828
);
2929

3030
public $message = 'This value is not a valid locale.';
31+
public $canonicalize = false;
3132
}

src/Symfony/Component/Validator/Constraints/LocaleValidator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public function validate($value, Constraint $constraint)
4141
}
4242

4343
$value = (string) $value;
44+
if ($constraint->canonicalize) {
45+
$value = \Locale::canonicalize($value);
46+
}
4447
$locales = Intl::getLocaleBundle()->getLocaleNames();
4548
$aliases = Intl::getLocaleBundle()->getAliases();
4649

src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,45 @@ public function getInvalidLocales()
9090
array('foobar'),
9191
);
9292
}
93+
94+
/**
95+
* @dataProvider getUncanonicalizedLocales
96+
*/
97+
public function testInvalidLocalesWithoutCanonicalization(string $locale)
98+
{
99+
$constraint = new Locale(array(
100+
'message' => 'myMessage',
101+
));
102+
103+
$this->validator->validate($locale, $constraint);
104+
105+
$this->buildViolation('myMessage')
106+
->setParameter('{{ value }}', '"'.$locale.'"')
107+
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
108+
->assertRaised();
109+
}
110+
111+
/**
112+
* @dataProvider getUncanonicalizedLocales
113+
*/
114+
public function testValidLocalesWithCanonicalization(string $locale)
115+
{
116+
$constraint = new Locale(array(
117+
'message' => 'myMessage',
118+
'canonicalize' => true,
119+
));
120+
121+
$this->validator->validate($locale, $constraint);
122+
123+
$this->assertNoViolation();
124+
}
125+
126+
public function getUncanonicalizedLocales(): iterable
127+
{
128+
return array(
129+
array('en-US'),
130+
array('es-AR'),
131+
array('fr_FR.utf8'),
132+
);
133+
}
93134
}

0 commit comments

Comments
 (0)
0