Skip to content

Commit d6f34a5

Browse files
maxperrimondfabpot
authored andcommitted
[Validator] Add alpha3 option to country constraint
1 parent 81abb4e commit d6f34a5

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

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

77
* added the `Hostname` constraint and validator
8+
* added option `alpha3` to `Country` constraint
89

910
5.0.0
1011
-----

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

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

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

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

src/Symfony/Component/Validator/Constraints/CountryValidator.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 (!Countries::exists($value)) {
46+
if ($constraint->alpha3 ? !Countries::alpha3CodeExists($value) : !Countries::exists($value)) {
4747
$this->context->buildViolation($constraint->message)
4848
->setParameter('{{ value }}', $this->formatValue($value))
4949
->setCode(Country::NO_SUCH_COUNTRY_ERROR)

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

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

106+
/**
107+
* @dataProvider getValidAlpha3Countries
108+
*/
109+
public function testValidAlpha3Countries($country)
110+
{
111+
$this->validator->validate($country, new Country([
112+
'alpha3' => true,
113+
]));
114+
115+
$this->assertNoViolation();
116+
}
117+
118+
public function getValidAlpha3Countries()
119+
{
120+
return [
121+
['GBR'],
122+
['ATA'],
123+
['MYT'],
124+
];
125+
}
126+
127+
/**
128+
* @dataProvider getInvalidAlpha3Countries
129+
*/
130+
public function testInvalidAlpha3Countries($country)
131+
{
132+
$constraint = new Country([
133+
'alpha3' => true,
134+
'message' => 'myMessage',
135+
]);
136+
137+
$this->validator->validate($country, $constraint);
138+
139+
$this->buildViolation('myMessage')
140+
->setParameter('{{ value }}', '"'.$country.'"')
141+
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
142+
->assertRaised();
143+
}
144+
145+
public function getInvalidAlpha3Countries()
146+
{
147+
return [
148+
['foobar'],
149+
['GB'],
150+
['ZZZ'],
151+
['zzz'],
152+
];
153+
}
154+
106155
public function testValidateUsingCountrySpecificLocale()
107156
{
108157
// in order to test with "en_GB"

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