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 97f8d00

Browse filesBrowse files
committed
[Validator] Add alpha3 option to country constraint
1 parent 9f74fb0 commit 97f8d00
Copy full SHA for 97f8d00

File tree

4 files changed

+51
-1
lines changed
Filter options

4 files changed

+51
-1
lines changed

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

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

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

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

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