diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 237dc68147b84..02e88227b7880 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.4.0 +----- + + * added support for checking an array of types in `TypeValidator` + 4.3.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/TypeValidator.php b/src/Symfony/Component/Validator/Constraints/TypeValidator.php index 206836d3617fc..ebcf50165e14d 100644 --- a/src/Symfony/Component/Validator/Constraints/TypeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TypeValidator.php @@ -33,22 +33,25 @@ public function validate($value, Constraint $constraint) return; } - $type = strtolower($constraint->type); - $type = 'boolean' == $type ? 'bool' : $constraint->type; - $isFunction = 'is_'.$type; - $ctypeFunction = 'ctype_'.$type; - - if (\function_exists($isFunction) && $isFunction($value)) { - return; - } elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) { - return; - } elseif ($value instanceof $constraint->type) { - return; + $types = (array) $constraint->type; + + foreach ($types as $type) { + $type = strtolower($type); + $type = 'boolean' === $type ? 'bool' : $type; + $isFunction = 'is_'.$type; + $ctypeFunction = 'ctype_'.$type; + if (\function_exists($isFunction) && $isFunction($value)) { + return; + } elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) { + return; + } elseif ($value instanceof $type) { + return; + } } $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) - ->setParameter('{{ type }}', $constraint->type) + ->setParameter('{{ type }}', implode('|', $types)) ->setCode(Type::INVALID_TYPE_ERROR) ->addViolation(); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php index 17334bea7925a..af032ef761d16 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php @@ -163,6 +163,52 @@ public function getInvalidValues() ]; } + /** + * @dataProvider getValidValuesMultipleTypes + */ + public function testValidValuesMultipleTypes($value, array $types) + { + $constraint = new Type(['type' => $types]); + + $this->validator->validate($value, $constraint); + + $this->assertNoViolation(); + } + + public function getValidValuesMultipleTypes() + { + return [ + ['12345', ['array', 'string']], + [[], ['array', 'string']], + ]; + } + + /** + * @dataProvider getInvalidValuesMultipleTypes + */ + public function testInvalidValuesMultipleTypes($value, $types, $valueAsString) + { + $constraint = new Type([ + 'type' => $types, + 'message' => 'myMessage', + ]); + + $this->validator->validate($value, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', $valueAsString) + ->setParameter('{{ type }}', implode('|', $types)) + ->setCode(Type::INVALID_TYPE_ERROR) + ->assertRaised(); + } + + public function getInvalidValuesMultipleTypes() + { + return [ + ['12345', ['boolean', 'array'], '"12345"'], + ]; + } + protected function createFile() { if (!static::$file) {