diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 1dea1e7fa1..d2eb00fc42 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -5,7 +5,7 @@ CHANGELOG ----- * added the `Hostname` constraint and validator - * added option `alpha3` to `Country` constraint + * added the `alpha3` option to the `Country` and `Language` constraints * allow to define a reusable set of constraints by extending the `Compound` constraint * added `Sequentially` constraint, to sequentially validate a set of constraints (any violation raised will prevent further validation of the nested constraints) * added the `divisibleBy` option to the `Count` constraint diff --git a/src/Symfony/Component/Validator/Constraints/Language.php b/src/Symfony/Component/Validator/Constraints/Language.php index 6b3ac769f9..1eac3126f2 100644 --- a/src/Symfony/Component/Validator/Constraints/Language.php +++ b/src/Symfony/Component/Validator/Constraints/Language.php @@ -30,6 +30,7 @@ class Language extends Constraint ]; public $message = 'This value is not a valid language.'; + public $alpha3 = false; public function __construct($options = null) { diff --git a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php index 54cd07da18..911a713314 100644 --- a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php @@ -43,7 +43,7 @@ class LanguageValidator extends ConstraintValidator $value = (string) $value; - if (!Languages::exists($value)) { + if ($constraint->alpha3 ? !Languages::alpha3CodeExists($value) : !Languages::exists($value)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Language::NO_SUCH_LANGUAGE_ERROR) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php index 669e7d2d97..73584a7e9c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php @@ -102,6 +102,55 @@ class LanguageValidatorTest extends ConstraintValidatorTestCase ]; } + /** + * @dataProvider getValidAlpha3Languages + */ + public function testValidAlpha3Languages($language) + { + $this->validator->validate($language, new Language([ + 'alpha3' => true, + ])); + + $this->assertNoViolation(); + } + + public function getValidAlpha3Languages() + { + return [ + ['deu'], + ['eng'], + ['fra'], + ]; + } + + /** + * @dataProvider getInvalidAlpha3Languages + */ + public function testInvalidAlpha3Languages($language) + { + $constraint = new Language([ + 'alpha3' => true, + 'message' => 'myMessage', + ]); + + $this->validator->validate($language, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$language.'"') + ->setCode(Language::NO_SUCH_LANGUAGE_ERROR) + ->assertRaised(); + } + + public function getInvalidAlpha3Languages() + { + return [ + ['foobar'], + ['en'], + ['ZZZ'], + ['zzz'], + ]; + } + public function testValidateUsingCountrySpecificLocale() { IntlTestHelper::requireFullIntl($this, false);