8000 [Validator] add alpha3 option to Language constraint by xabbuh · Pull Request #35848 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] add alpha3 option to Language constraint #35848

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
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add alpha3 option to Language constraint
  • Loading branch information
xabbuh committed Feb 24, 2020
commit ce73b98e2cadd0aa80c84fc395994d25c2597cbb
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
-----

* added the `Hostname` constraint and validator
* added option `alpha3` to `Country` constraint
* added the `alpha3` option to the `Country` and `Language` constraints
* allow to define a reusable set of constraints by extending the `Compound` constraint
* added `Sequentially` constraint, to sequentially validate a set of constraints (any violation raised will prevent further validation of the nested constraints)
* added the `divisibleBy` option to the `Count` constraint
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Language extends Constraint
];

public $message = 'This value is not a valid language.';
public $alpha3 = false;

public function __construct($options = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function validate($value, Constraint $constraint)

$value = (string) $value;

if (!Languages::exists($value)) {
if ($constraint->alpha3 ? !Languages::alpha3CodeExists($value) : !Languages::exists($value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Language::NO_SUCH_LANGUAGE_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,55 @@ public function getInvalidLanguages()
];
}

/**
* @dataProvider getValidAlpha3Languages
*/
public function testValidAlpha3Languages($language)
{
$this->validator->validate($language, new Language([
'alpha3' => true,
]));

$this->assertNoViolation();
}

public function getValidAlpha3Languages()
{
return [
['deu'],
['eng'],
['fra'],
];
}

/**
* @dataProvider getInvalidAlpha3Languages
*/
public function testInvalidAlpha3Languages($language)
{
$constraint = new Language([
'alpha3' => true,
'message' => 'myMessage',
]);

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

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$language.'"')
->setCode(Language::NO_SUCH_LANGUAGE_ERROR)
->assertRaised();
}

public function getInvalidAlpha3Languages()
{
return [
['foobar'],
['en'],
['ZZZ'],
['zzz'],
];
}

public function testValidateUsingCountrySpecificLocale()
{
IntlTestHelper::requireFullIntl($this, false);
Expand Down
0