Navigation Menu

Skip to content

Commit

Permalink
feature #22353 [Validator] Add canonicalize option for Locale val…
Browse files Browse the repository at this point in the history
…idator (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
  • Loading branch information
fabpot committed Feb 7, 2018
2 parents c557327 + 39dfa3d commit 5f0c279
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/Locale.php
Expand Up @@ -28,4 +28,5 @@ class Locale extends Constraint
);

public $message = 'This value is not a valid locale.';
public $canonicalize = false;
}
Expand Up @@ -41,6 +41,9 @@ public function validate($value, Constraint $constraint)
}

$value = (string) $value;
if ($constraint->canonicalize) {
$value = \Locale::canonicalize($value);
}
$locales = Intl::getLocaleBundle()->getLocaleNames();
$aliases = Intl::getLocaleBundle()->getAliases();

Expand Down
Expand Up @@ -90,4 +90,45 @@ public function getInvalidLocales()
array('foobar'),
);
}

/**
* @dataProvider getUncanonicalizedLocales
*/
public function testInvalidLocalesWithoutCanonicalization(string $locale)
{
$constraint = new Locale(array(
'message' => 'myMessage',
));

$this->validator->validate($locale, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$locale.'"')
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
->assertRaised();
}

/**
* @dataProvider getUncanonicalizedLocales
*/
public function testValidLocalesWithCanonicalization(string $locale)
{
$constraint = new Locale(array(
'message' => 'myMessage',
'canonicalize' => true,
));

$this->validator->validate($locale, $constraint);

$this->assertNoViolation();
}

public function getUncanonicalizedLocales(): iterable
{
return array(
array('en-US'),
array('es-AR'),
array('fr_FR.utf8'),
);
}
}

0 comments on commit 5f0c279

Please sign in to comment.