catch any UnexpectedValueException on validation

This commit is contained in:
Christian Flothmann 2018-07-10 15:47:44 +02:00
parent b452c015cc
commit fa3586029f
48 changed files with 112 additions and 49 deletions

View File

@ -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`

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 <bschussek@gmail.com>
@ -34,7 +35,7 @@ class AllValidator extends ConstraintValidator
}
if (!\is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
throw new UnexpectedValueException($value, 'iterable');
}
$context = $this->context;

View File

@ -15,7 +15,7 @@ use Symfony\Component\Intl\Intl;
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 <michael.vhirsch@gmail.com>
@ -34,7 +34,7 @@ class BicValidator extends ConstraintValidator
}
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);

View File

@ -15,6 +15,7 @@ use Symfony\Component\Validator\Constraint;
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 @@ class ChoiceValidator extends ConstraintValidator
}
if ($constraint->multiple && !\is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
throw new UnexpectedValueException($value, 'array');
}
if ($constraint->callback) {

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 <bschussek@gmail.com>
@ -34,7 +35,7 @@ class CollectionValidator extends ConstraintValidator
}
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

View File

@ -13,7 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
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 <bschussek@gmail.com>
@ -30,7 +30,7 @@ class CountValidator extends ConstraintValidator
}
if (!\is_array($value) && !$value instanceof \Countable) {
throw new UnexpectedTypeException($value, 'array or \Countable');
throw new UnexpectedValueException($value, 'array|\Countable');
}
$count = \count($value);

View File

@ -16,6 +16,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;
/**
* Validates whether a value is a valid country code.
@ -38,7 +39,7 @@ class CountryValidator extends ConstraintValidator
}
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)) {

View File

@ -16,6 +16,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;
/**
* Validates whether a value is a valid currency.
@ -39,7 +40,7 @@ class CurrencyValidator extends ConstraintValidator
}
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)) {

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -40,7 +41,7 @@ class DateTimeValidator extends DateValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 <bschussek@gmail.com>
@ -58,7 +59,7 @@ class DateValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -17,6 +17,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 Bernhard Schussek <bschussek@gmail.com>
@ -75,7 +76,7 @@ class EmailValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
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 <bschussek@gmail.com>
@ -114,7 +115,7 @@ class FileValidator extends ConstraintValidator
}
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;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 <manu@sprain.ch>
@ -149,7 +150,7 @@ class IbanValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 @@ class IpValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 @@ class IsbnValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 @@ class IssnValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -16,6 +16,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;
/**
* Validates whether a value is a valid language code.
@ -38,7 +39,7 @@ class LanguageValidator extends ConstraintValidator
}
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)) {

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 <bschussek@gmail.com>
@ -34,7 +35,7 @@ class LengthValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$stringValue = (string) $value;

View File

@ -15,6 +15,7 @@ use Symfony\Component\Intl\Intl;
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 @@ class LocaleValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$inputValue = (string) $value;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 @@ class LuhnValidator extends ConstraintValidator
// 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;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 @@ class RegexValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 <bschussek@gmail.com>
@ -58,7 +59,7 @@ class TimeValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -15,6 +15,7 @@ use Symfony\Component\Validator\Constraint;
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 <bschussek@gmail.com>
@ -53,7 +54,7 @@ class UrlValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Constraints;
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 @@ class UuidValidator extends ConstraintValidator
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;

View File

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <christian.flothmann@sensiolabs.de>
*/
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;
}
}

View File

@ -32,7 +32,7 @@ class AllValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testThrowsExceptionIfNotTraversable()
{

View File

@ -37,7 +37,7 @@ class BicValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -38,7 +38,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectArrayIfMultipleIsTrue()
{

View File

@ -53,7 +53,7 @@ abstract class CollectionValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testThrowsExceptionIfNotTraversable()
{

View File

@ -35,7 +35,7 @@ abstract class CountValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsCountableType()
{

View File

@ -38,7 +38,7 @@ class CountryValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -38,7 +38,7 @@ class CurrencyValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -59,7 +59,7 @@ class DateTimeValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -59,7 +59,7 @@ class DateValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -63,7 +63,7 @@ class EmailValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -67,7 +67,7 @@ abstract class FileValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleTypeOrFile()
{

View File

@ -37,7 +37,7 @@ class IpValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -137,7 +137,7 @@ class IsbnValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -107,7 +107,7 @@ class IssnValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -38,7 +38,7 @@ class LanguageValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -37,7 +37,7 @@ class LengthValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -65,7 +65,7 @@ class LocaleValidatorTest extends ConstraintValidatorTestCase
/**
* @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 @@ class LocaleValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -99,7 +99,7 @@ class LuhnValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
* @dataProvider getInvalidTypes
*/
public function testInvalidTypes($number)

View File

@ -37,7 +37,7 @@ class RegexValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -48,7 +48,7 @@ class TimeValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -48,7 +48,7 @@ class UrlValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -50,7 +50,7 @@ class UuidValidatorTest extends ConstraintValidatorTestCase
}
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{

View File

@ -19,6 +19,7 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
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 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
$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();
}
}
}
}