Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce73b98

Browse filesBrowse files
committed
add alpha3 option to Language constraint
1 parent b3b368b commit ce73b98
Copy full SHA for ce73b98

File tree

4 files changed

+52
-2
lines changed
Filter options

4 files changed

+52
-2
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Language.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/LanguageValidator.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php
+49Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.