ValidatorException example

$getMethod = 'get'.ucfirst($property);
            $isMethod = 'is'.ucfirst($property);
            $hasMethod = 'has'.ucfirst($property);

            if (method_exists($class$getMethod)) {
                $method = $getMethod;
            } elseif (method_exists($class$isMethod)) {
                $method = $isMethod;
            } elseif (method_exists($class$hasMethod)) {
                $method = $hasMethod;
            } else {
                throw new ValidatorException(sprintf('Neither of these methods exist in class "%s": "%s", "%s", "%s".', $class$getMethod$isMethod$hasMethod));
            }
        } elseif (!method_exists($class$method)) {
            throw new ValidatorException(sprintf('The "%s()" method does not exist in class "%s".', $method$class));
        }

        parent::__construct($class$method$property);
    }

    public function getPropertyValue(mixed $object): mixed
    {
        return $this->newReflectionMember($object)->invoke($object);
    }
class PropertyMetadata extends MemberMetadata
{
    /** * @param string $class The class this property is defined on * @param string $name The name of this property * * @throws ValidatorException */
    public function __construct(string $class, string $name)
    {
        if (!property_exists($class$name)) {
            throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $name$class));
        }

        parent::__construct($class$name$name);
    }

    public function getPropertyValue(mixed $object): mixed
    {
        $reflProperty = $this->getReflectionMember($object);

        if ($reflProperty->hasType() && !$reflProperty->isInitialized($object)) {
            // There is no way to check if a property has been unset or if it is uninitialized.

    public function getInstance(Constraint $constraint): ConstraintValidatorInterface
    {
        $name = $constraint->validatedBy();

        if (!isset($this->validators[$name])) {
            if ($this->container->has($name)) {
                $this->validators[$name] = $this->container->get($name);
            } else {
                if (!class_exists($name)) {
                    throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "%s".', $nameget_debug_type($constraint)));
                }

                $this->validators[$name] = new $name();
            }
        }

        if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
            throw new UnexpectedTypeException($this->validators[$name], ConstraintValidatorInterface::class);
        }

        return $this->validators[$name];
    }
return $this;
        }

        throw new RuntimeException(sprintf('Cannot validate values of type "%s" automatically. Please provide a constraint.', get_debug_type($value)));
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null)static
    {
        $classMetadata = $this->metadataFactory->getMetadataFor($object);

        if (!$classMetadata instanceof ClassMetadataInterface) {
            throw new ValidatorException(sprintf('The metadata factory should return instances of "\Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', get_debug_type($classMetadata)));
        }

        $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
        $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
        $cacheKey = $this->generateCacheKey($object);
        $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);

        $previousValue = $this->context->getValue();
        $previousObject = $this->context->getObject();
        $previousMetadata = $this->context->getMetadata();
        $previousPath = $this->context->getPropertyPath();
        
$getMethod = 'get'.ucfirst($property);
            $isMethod = 'is'.ucfirst($property);
            $hasMethod = 'has'.ucfirst($property);

            if (method_exists($class$getMethod)) {
                $method = $getMethod;
            } elseif (method_exists($class$isMethod)) {
                $method = $isMethod;
            } elseif (method_exists($class$hasMethod)) {
                $method = $hasMethod;
            } else {
                throw new ValidatorException(sprintf('Neither of these methods exist in class "%s": "%s", "%s", "%s".', $class$getMethod$isMethod$hasMethod));
            }
        } elseif (!method_exists($class$method)) {
            throw new ValidatorException(sprintf('The "%s()" method does not exist in class "%s".', $method$class));
        }

        parent::__construct($class$method$property);
    }

    public function getPropertyValue(mixed $object): mixed
    {
        return $this->newReflectionMember($object)->invoke($object);
    }
return $this;
    }

    /** * Adds an XML constraint mapping file to the validator. * * @return $this */
    public function addXmlMapping(string $path)static
    {
        if (null !== $this->metadataFactory) {
            throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
        }

        $this->xmlMappings[] = $path;

        return $this;
    }

    /** * Adds a list of XML constraint mapping files to the validator. * * @param string[] $paths The paths to the mapping files * * @return $this */
return $this;
    }

    /** * Adds an XML constraint mapping file to the validator. * * @return $this */
    public function addXmlMapping(string $path)static
    {
        if (null !== $this->metadataFactory) {
            throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
        }

        $this->xmlMappings[] = $path;

        return $this;
    }

    /** * Adds a list of XML constraint mapping files to the validator. * * @param string[] $paths The paths to the mapping files * * @return $this */

    public function getInstance(Constraint $constraint): ConstraintValidatorInterface
    {
        $name = $constraint->validatedBy();

        if (!isset($this->validators[$name])) {
            if ($this->container->has($name)) {
                $this->validators[$name] = $this->container->get($name);
            } else {
                if (!class_exists($name)) {
                    throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "%s".', $nameget_debug_type($constraint)));
                }

                $this->validators[$name] = new $name();
            }
        }

        if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
            throw new UnexpectedTypeException($this->validators[$name], ConstraintValidatorInterface::class);
        }

        return $this->validators[$name];
    }
return $this;
        }

        throw new RuntimeException(sprintf('Cannot validate values of type "%s" automatically. Please provide a constraint.', get_debug_type($value)));
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null)static
    {
        $classMetadata = $this->metadataFactory->getMetadataFor($object);

        if (!$classMetadata instanceof ClassMetadataInterface) {
            throw new ValidatorException(sprintf('The metadata factory should return instances of "\Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', get_debug_type($classMetadata)));
        }

        $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
        $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
        $cacheKey = $this->generateCacheKey($object);
        $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);

        $previousValue = $this->context->getValue();
        $previousObject = $this->context->getObject();
        $previousMetadata = $this->context->getMetadata();
        $previousPath = $this->context->getPropertyPath();
        
class PropertyMetadata extends MemberMetadata
{
    /** * @param string $class The class this property is defined on * @param string $name The name of this property * * @throws ValidatorException */
    public function __construct(string $class, string $name)
    {
        if (!property_exists($class$name)) {
            throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $name$class));
        }

        parent::__construct($class$name$name);
    }

    public function getPropertyValue(mixed $object): mixed
    {
        $reflProperty = $this->getReflectionMember($object);

        if ($reflProperty->hasType() && !$reflProperty->isInitialized($object)) {
            // There is no way to check if a property has been unset or if it is uninitialized.
Home | Imprint | This part of the site doesn't use cookies.