Skip to content

Commit ce73b98

Browse files
committed
add alpha3 option to Language constraint
1 parent b3b368b commit ce73b98

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANGELOG
55
-----
66

77
* added the `Hostname` constraint and validator
8-
* added option `alpha3` to `Country` constraint
8+
* added the `alpha3` option to the `Country` and `Language` constraints
99
* allow to define a reusable set of constraints by extending the `Compound` constraint
1010
* added `Sequentially` constraint, to sequentially validate a set of constraints (any violation raised will prevent further validation of the nested constraints)
1111
* added the `divisibleBy` option to the `Count` constraint

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Language extends Constraint
3030
];
3131

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

3435
public function __construct($options = null)
3536
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function validate($value, Constraint $constraint)
4343

4444
$value = (string) $value;
4545

46-
if (!Languages::exists($value)) {
46+
if ($constraint->alpha3 ? !Languages::alpha3CodeExists($value) : !Languages::exists($value)) {
4747
$this->context->buildViolation($constraint->message)
4848
->setParameter('{{ value }}', $this->formatValue($value))
4949
->setCode(Language::NO_SUCH_LANGUAGE_ERROR)

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,55 @@ public function getInvalidLanguages()
102102
];
103103
}
104104

105+
/**
106+
* @dataProvider getValidAlpha3Languages
107+
*/
108+
public function testValidAlpha3Languages($language)
109+
{
110+
$this->validator->validate($language, new Language([
111+
'alpha3' => true,
112+
]));
113+
114+
$this->assertNoViolation();
115+
}
116+
117+
public function getValidAlpha3Languages()
118+
{
119+
return [
120+
['deu'],
121+
['eng'],
122+
['fra'],
123+
];
124+
}
125+
126+
/**
127+
* @dataProvider getInvalidAlpha3Languages
128+
*/
129+
public function testInvalidAlpha3Languages($language)
130+
{
131+
$constraint = new Language([
132+
'alpha3' => true,
133+
'message' => 'myMessage',
134+
]);
135+
136+
$this->validator->validate($language, $constraint);
137+
138+
$this->buildViolation('myMessage')
139+
->setParameter('{{ value }}', '"'.$language.'"')
140+
->setCode(Language::NO_SUCH_LANGUAGE_ERROR)
141+
->assertRaised();
142+
}
143+
144+
public function getInvalidAlpha3Languages()
145+
{
146+
return [
147+
['foobar'],
148+
['en'],
149+
['ZZZ'],
150+
['zzz'],
151+
];
152+
}
153+
105154
public function testValidateUsingCountrySpecificLocale()
106155
{
107156
IntlTestHelper::requireFullIntl($this, false);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy