8000 [Validator] Add alpha3 option to country constraint · symfony/symfony@374f110 · GitHub
[go: up one dir, main page]

Skip to content

Commit 374f110

Browse files
committed
[Validator] Add alpha3 option to country constraint
1 parent 0c6d64b commit 374f110

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
< 8000 button class="Button Button--iconOnly Button--invisible" aria-label="More options" id=":R14ctlab:" aria-haspopup="true" aria-expanded="false" tabindex="0">
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CHANGELOG
1717
* changed the default value of `Length::$allowEmptyString` to `false` and made it optional
1818
* removed `Symfony\Component\Validator\Mapping\Cache\CacheInterface` in favor of PSR-6.
1919
* removed `ValidatorBuilder::setMetadataCache`, use `ValidatorBuilder::setMappingCache` instead.
20+
* added option `alpha3` to `Country` constraint
2021

2122
4.4.0
2223
-----

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function validate($value, Constraint $constraint)
4343

4444
$value = (string) $value;
4545

46-
if (!Countries::exists($value)) {
46+
if ((!$constraint->alpha3 && !Countries::exists($value)) ||
47+
($constraint->alpha3 && !Countries::alpha3CodeExists($value))) {
4748
$this->context->buildViolation($constraint->message)
4849
->setParameter('{{ value }}', $this->formatValue($value))
4950
->setCode(Country::NO_SUCH_COUNTRY_ERROR)

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,53 @@ 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+
];
151+
}
152+
106153
public function testValidateUsingCountrySpecificLocale()
107154
{
108155
// in order to test with "en_GB"

0 commit comments

Comments
 (0)
0