feature #35116 [Validator] Add alpha3 option to country constraint (maxperrimond)
This PR was squashed before being merged into the 5.1-dev branch (closes #35116).
Discussion
----------
[Validator] Add alpha3 option to country constraint
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets |
| License | MIT
| Doc PR | https://github.com/symfony/symfony-docs/pull/12857
A following of #33791 and #32988 to add `alpha3` option also to `Country` constraint in the validator component.
Commits
-------
d6f34a5df6
[Validator] Add alpha3 option to country constraint
This commit is contained in:
commit
1bb485b1c7
@ -5,6 +5,7 @@ CHANGELOG
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
* added the `Hostname` constraint and validator
|
* added the `Hostname` constraint and validator
|
||||||
|
* added option `alpha3` to `Country` constraint
|
||||||
|
|
||||||
5.0.0
|
5.0.0
|
||||||
-----
|
-----
|
||||||
|
@ -30,6 +30,7 @@ class Country extends Constraint
|
|||||||
];
|
];
|
||||||
|
|
||||||
public $message = 'This value is not a valid country.';
|
public $message = 'This value is not a valid country.';
|
||||||
|
public $alpha3 = false;
|
||||||
|
|
||||||
public function __construct($options = null)
|
public function __construct($options = null)
|
||||||
{
|
{
|
||||||
|
@ -43,7 +43,7 @@ class CountryValidator extends ConstraintValidator
|
|||||||
|
|
||||||
$value = (string) $value;
|
$value = (string) $value;
|
||||||
|
|
||||||
if (!Countries::exists($value)) {
|
if ($constraint->alpha3 ? !Countries::alpha3CodeExists($value) : !Countries::exists($value)) {
|
||||||
$this->context->buildViolation($constraint->message)
|
$this->context->buildViolation($constraint->message)
|
||||||
->setParameter('{{ value }}', $this->formatValue($value))
|
->setParameter('{{ value }}', $this->formatValue($value))
|
||||||
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
|
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
|
||||||
|
@ -103,6 +103,55 @@ class CountryValidatorTest extends ConstraintValidatorTestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getValidAlpha3Countries
|
||||||
|
*/
|
||||||
|
public function testValidAlpha3Countries($country)
|
||||||
|
{
|
||||||
|
$this->validator->validate($country, new Country([
|
||||||
|
'alpha3' => true,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValidAlpha3Countries()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['GBR'],
|
||||||
|
['ATA'],
|
||||||
|
['MYT'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getInvalidAlpha3Countries
|
||||||
|
*/
|
||||||
|
public function testInvalidAlpha3Countries($country)
|
||||||
|
{
|
||||||
|
$constraint = new Country([
|
||||||
|
'alpha3' => true,
|
||||||
|
'message' => 'myMessage',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->validator->validate($country, $constraint);
|
||||||
|
|
||||||
|
$this->buildViolation('myMessage')
|
||||||
|
->setParameter('{{ value }}', '"'.$country.'"')
|
||||||
|
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInvalidAlpha3Countries()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['foobar'],
|
||||||
|
['GB'],
|
||||||
|
['ZZZ'],
|
||||||
|
['zzz'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function testValidateUsingCountrySpecificLocale()
|
public function testValidateUsingCountrySpecificLocale()
|
||||||
{
|
{
|
||||||
// in order to test with "en_GB"
|
// in order to test with "en_GB"
|
||||||
|
Reference in New Issue
Block a user