diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index e69e0e932f6c7..3863b5717a1fe 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -4,6 +4,8 @@ CHANGELOG 4.2.0 ----- + * added a new `UnexpectedValueException` that can be thrown by constraint validators, these exceptions are caught by + the validator and are converted into constraint violations * added `DivisibleBy` constraint * decoupled from `symfony/translation` by using `Symfony\Contracts\Translation\TranslatorInterface` * deprecated `ValidatorBuilderInterface` diff --git a/src/Symfony/Component/Validator/Constraints/AllValidator.php b/src/Symfony/Component/Validator/Constraints/AllValidator.php index 97de37058f650..5bacdf8b95ff2 100644 --- a/src/Symfony/Component/Validator/Constraints/AllValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AllValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint) } if (!\is_array($value) && !$value instanceof \Traversable) { - throw new UnexpectedTypeException($value, 'array or Traversable'); + throw new UnexpectedValueException($value, 'iterable'); } $context = $this->context; diff --git a/src/Symfony/Component/Validator/Constraints/BicValidator.php b/src/Symfony/Component/Validator/Constraints/BicValidator.php index 89077db68038a..7daf9a9c250d8 100644 --- a/src/Symfony/Component/Validator/Constraints/BicValidator.php +++ b/src/Symfony/Component/Validator/Constraints/BicValidator.php @@ -15,7 +15,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\LogicException; -use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Michael Hirschler @@ -34,7 +34,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $canonicalize = str_replace(' ', '', $value); diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index e40b4d9ecf930..19eae97481e2c 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -15,6 +15,7 @@ use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * ChoiceValidator validates that the value is one of the expected values. @@ -43,7 +44,7 @@ public function validate($value, Constraint $constraint) } if ($constraint->multiple && !\is_array($value)) { - throw new UnexpectedTypeException($value, 'array'); + throw new UnexpectedValueException($value, 'array'); } if ($constraint->callback) { diff --git a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php index e3a297329d1c8..731501e08db14 100644 --- a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint) } if (!\is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) { - throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess'); + throw new UnexpectedValueException($value, 'array|(Traversable&ArrayAccess)'); } // We need to keep the initialized context when CollectionValidator diff --git a/src/Symfony/Component/Validator/Constraints/CountValidator.php b/src/Symfony/Component/Validator/Constraints/CountValidator.php index 39be8aa82e9a7..c60912093d7be 100644 --- a/src/Symfony/Component/Validator/Constraints/CountValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountValidator.php @@ -13,7 +13,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; -use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -30,7 +30,7 @@ public function validate($value, Constraint $constraint) } if (!\is_array($value) && !$value instanceof \Countable) { - throw new UnexpectedTypeException($value, 'array or \Countable'); + throw new UnexpectedValueException($value, 'array|\Countable'); } $count = \count($value); diff --git a/src/Symfony/Component/Validator/Constraints/CountryValidator.php b/src/Symfony/Component/Validator/Constraints/CountryValidator.php index 2cc057d1c266b..ad5dc36a9ab54 100644 --- a/src/Symfony/Component/Validator/Constraints/CountryValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountryValidator.php @@ -16,6 +16,7 @@ use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\LogicException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether a value is a valid country code. @@ -38,7 +39,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } if (!class_exists(Intl::class)) { diff --git a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php index bcd9e8f007959..44e5df246cb89 100644 --- a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php @@ -16,6 +16,7 @@ use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\LogicException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether a value is a valid currency. @@ -39,7 +40,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } if (!class_exists(Intl::class)) { diff --git a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php index ac6bf975d27dc..16abf6f671ce4 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php @@ -13,6 +13,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -40,7 +41,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/DateValidator.php b/src/Symfony/Component/Validator/Constraints/DateValidator.php index 7b5825408aa05..93b1f7d074e18 100644 --- a/src/Symfony/Component/Validator/Constraints/DateValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -58,7 +59,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/EmailValidator.php b/src/Symfony/Component/Validator/Constraints/EmailValidator.php index d373368583dd0..d79440a6d4cf0 100644 --- a/src/Symfony/Component/Validator/Constraints/EmailValidator.php +++ b/src/Symfony/Component/Validator/Constraints/EmailValidator.php @@ -17,6 +17,7 @@ use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\LogicException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -75,7 +76,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index b2a850cd286bb..047254632bd17 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -16,6 +16,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -114,7 +115,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !$value instanceof FileObject && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $path = $value instanceof FileObject ? $value->getPathname() : (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/IbanValidator.php b/src/Symfony/Component/Validator/Constraints/IbanValidator.php index 9d8d5b863cc86..8f7cb5e910983 100644 --- a/src/Symfony/Component/Validator/Constraints/IbanValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IbanValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Manuel Reinhard @@ -149,7 +150,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/IpValidator.php b/src/Symfony/Component/Validator/Constraints/IpValidator.php index 5c687b900fdbb..b96e50623cc62 100644 --- a/src/Symfony/Component/Validator/Constraints/IpValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IpValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether a value is a valid IP address. @@ -37,7 +38,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/IsbnValidator.php b/src/Symfony/Component/Validator/Constraints/IsbnValidator.php index 9f0d1fa1a35a1..53687ddc80477 100644 --- a/src/Symfony/Component/Validator/Constraints/IsbnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IsbnValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether the value is a valid ISBN-10 or ISBN-13. @@ -40,7 +41,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/IssnValidator.php b/src/Symfony/Component/Validator/Constraints/IssnValidator.php index f045e62e2ca18..90dceba1faa2b 100644 --- a/src/Symfony/Component/Validator/Constraints/IssnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IssnValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether the value is a valid ISSN. @@ -39,7 +40,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php index 575e6c14ceda3..22ddb0fe53459 100644 --- a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php @@ -16,6 +16,7 @@ use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\LogicException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether a value is a valid language code. @@ -38,7 +39,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } if (!class_exists(Intl::class)) { diff --git a/src/Symfony/Component/Validator/Constraints/LengthValidator.php b/src/Symfony/Component/Validator/Constraints/LengthValidator.php index d8cb09ccb65f8..2fb3dba7980f2 100644 --- a/src/Symfony/Component/Validator/Constraints/LengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LengthValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $stringValue = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php index cc23b3f1a4c43..d4bca35a70375 100644 --- a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php @@ -15,6 +15,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether a value is a valid locale code. @@ -37,7 +38,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $inputValue = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php index 554fc6f48b10b..c69057caa6aba 100644 --- a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates a PAN using the LUHN Algorithm. @@ -50,7 +51,7 @@ public function validate($value, Constraint $constraint) // Work with strings only, because long numbers are represented as floats // internally and don't work with strlen() if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/RegexValidator.php b/src/Symfony/Component/Validator/Constraints/RegexValidator.php index c6b70feb7f0e1..b88a5b1909369 100644 --- a/src/Symfony/Component/Validator/Constraints/RegexValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RegexValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether a value match or not given regexp pattern. @@ -37,7 +38,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/TimeValidator.php b/src/Symfony/Component/Validator/Constraints/TimeValidator.php index 9bdd541c68872..799dbc3108e0c 100644 --- a/src/Symfony/Component/Validator/Constraints/TimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimeValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -58,7 +59,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/UrlValidator.php b/src/Symfony/Component/Validator/Constraints/UrlValidator.php index 35c63a06dc9ca..2f7db080543d5 100644 --- a/src/Symfony/Component/Validator/Constraints/UrlValidator.php +++ b/src/Symfony/Component/Validator/Constraints/UrlValidator.php @@ -15,6 +15,7 @@ use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\InvalidOptionsException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * @author Bernhard Schussek @@ -53,7 +54,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Constraints/UuidValidator.php b/src/Symfony/Component/Validator/Constraints/UuidValidator.php index 38e9a0da8585e..79c35ddac7e47 100644 --- a/src/Symfony/Component/Validator/Constraints/UuidValidator.php +++ b/src/Symfony/Component/Validator/Constraints/UuidValidator.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; /** * Validates whether the value is a valid UUID (also known as GUID). @@ -75,7 +76,7 @@ public function validate($value, Constraint $constraint) } if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + throw new UnexpectedValueException($value, 'string'); } $value = (string) $value; diff --git a/src/Symfony/Component/Validator/Exception/UnexpectedValueException.php b/src/Symfony/Component/Validator/Exception/UnexpectedValueException.php new file mode 100644 index 0000000000000..7a7f7f7bf2915 --- /dev/null +++ b/src/Symfony/Component/Validator/Exception/UnexpectedValueException.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Exception; + +/** + * @author Christian Flothmann + */ +class UnexpectedValueException extends UnexpectedTypeException +{ + private $expectedType; + + public function __construct($value, string $expectedType) + { + parent::__construct($value, $expectedType); + + $this->expectedType = $expectedType; + } + + public function getExpectedType(): string + { + return $this->expectedType; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php index 2792dc4014a73..054b5949ce59b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php @@ -32,7 +32,7 @@ public function testNullIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testThrowsExceptionIfNotTraversable() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php index d135923f647ab..231843a792d41 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php @@ -37,7 +37,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 38607bb9a8891..f9db12200e6d5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -38,7 +38,7 @@ public function objectMethodCallback() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectArrayIfMultipleIsTrue() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php index 32aa8e359e771..d025e1d97bdd7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php @@ -53,7 +53,7 @@ public function testFieldsAsDefaultOption() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testThrowsExceptionIfNotTraversable() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php index d23188a098213..baa919df2e785 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php @@ -35,7 +35,7 @@ public function testNullIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsCountableType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php index 044a742db630a..30ab40a71308a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php @@ -38,7 +38,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php index 1286ff31f89f3..3a13775fe1b98 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php @@ -38,7 +38,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index 918d6577c928a..c1e9d3807fe0a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -59,7 +59,7 @@ public function testDateTimeImmutableClassIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php index bf9c103f5810b..340b4f63121f9 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php @@ -59,7 +59,7 @@ public function testDateTimeImmutableClassIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php index 5f3353bc5fe5a..8bc11d4fd7ae5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php @@ -63,7 +63,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index 1f63b0d1d96d8..13d7b24ce2d2f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -67,7 +67,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleTypeOrFile() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php index 985bd9577d2d5..1cd0a1b33c87c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php @@ -37,7 +37,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php index f810fc9820ae8..0b93388850c89 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php @@ -137,7 +137,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php index ca61ffac4c550..9f485d241cade 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php @@ -107,7 +107,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php index 2980f2a233193..4f9e8e52a5837 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php @@ -38,7 +38,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php index da98387ccb466..1d537e2db3b58 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -37,7 +37,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php index b9f23db8a8869..060d17a6432a5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php @@ -65,7 +65,7 @@ public function testEmptyStringIsValid() /** * @group legacy * @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead. - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testLegacyExpectsStringCompatibleType() { @@ -73,7 +73,7 @@ public function testLegacyExpectsStringCompatibleType() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php index 3ee04d7f46820..8427b66825b13 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php @@ -99,7 +99,7 @@ public function getInvalidNumbers() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException * @dataProvider getInvalidTypes */ public function testInvalidTypes($number) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php index 5194b0816ea39..777fa26693f56 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php @@ -37,7 +37,7 @@ public function testEmptyStringIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php index 3b5e1e594d4e7..b0ccc7795e4d6 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php @@ -48,7 +48,7 @@ public function testDateTimeClassIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php index 5e7c81e92a6ef..fd9d252f38da3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php @@ -48,7 +48,7 @@ public function testEmptyStringFromObjectIsValid() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php index a39ec93d6cdf6..a83a7599ae4ef 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php @@ -50,7 +50,7 @@ public function testExpectsUuidConstraintCompatibleType() } /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException */ public function testExpectsStringCompatibleType() { diff --git a/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php b/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php index f50f767bd9685..964052a341cb2 100644 --- a/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php +++ b/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php @@ -19,6 +19,7 @@ use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\NoSuchMetadataException; use Symfony\Component\Validator\Exception\RuntimeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; use Symfony\Component\Validator\Exception\UnsupportedMetadataException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Component\Validator\Mapping\CascadingStrategy; @@ -798,7 +799,14 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata, $validator = $this->validatorFactory->getInstance($constraint); $validator->initialize($context); - $validator->validate($value, $constraint); + + try { + $validator->validate($value, $constraint); + } catch (UnexpectedValueException $e) { + $context->buildViolation('This value should be of type {{ type }}.') + ->setParameter('{{ type }}', $e->getExpectedType()) + ->addViolation(); + } } } }