Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. RangeValidator.php

function RangeValidator::validate

Overrides ConstraintValidatorInterface::validate

File

vendor/symfony/validator/Constraints/RangeValidator.php, line 32

Class

RangeValidator
@author Bernhard Schussek <bschussek@gmail.com>

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate(mixed $value, Constraint $constraint) : void {
    if (!$constraint instanceof Range) {
        throw new UnexpectedTypeException($constraint, Range::class);
    }
    if (null === $value) {
        return;
    }
    $min = $this->getLimit($constraint->minPropertyPath, $constraint->min, $constraint);
    $max = $this->getLimit($constraint->maxPropertyPath, $constraint->max, $constraint);
    if (!is_numeric($value) && !$value instanceof \DateTimeInterface) {
        if ($this->isParsableDatetimeString($min) && $this->isParsableDatetimeString($max)) {
            $this->context
                ->buildViolation($constraint->invalidDateTimeMessage)
                ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
                ->setCode(Range::INVALID_CHARACTERS_ERROR)
                ->addViolation();
        }
        else {
            $this->context
                ->buildViolation($constraint->invalidMessage)
                ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
                ->setCode(Range::INVALID_CHARACTERS_ERROR)
                ->addViolation();
        }
        return;
    }
    // Convert strings to DateTimes if comparing another DateTime
    // This allows to compare with any date/time value supported by
    // the DateTime constructor:
    // https://php.net/datetime.formats
    if ($value instanceof \DateTimeInterface) {
        if (\is_string($min)) {
            try {
                $min = new $value($min);
            } catch (\Exception) {
                throw new ConstraintDefinitionException(\sprintf('The min value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $min, get_debug_type($value), get_debug_type($constraint)));
            }
        }
        if (\is_string($max)) {
            try {
                $max = new $value($max);
            } catch (\Exception) {
                throw new ConstraintDefinitionException(\sprintf('The max value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $max, get_debug_type($value), get_debug_type($constraint)));
            }
        }
    }
    $hasLowerLimit = null !== $min;
    $hasUpperLimit = null !== $max;
    if ($hasLowerLimit && $hasUpperLimit && ($value < $min || $value > $max)) {
        $message = $constraint->notInRangeMessage;
        $code = Range::NOT_IN_RANGE_ERROR;
        $violationBuilder = $this->context
            ->buildViolation($message)
            ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
            ->setParameter('{{ min }}', $this->formatValue($min, self::PRETTY_DATE))
            ->setParameter('{{ max }}', $this->formatValue($max, self::PRETTY_DATE))
            ->setCode($code);
        if (null !== $constraint->maxPropertyPath) {
            $violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath);
        }
        if (null !== $constraint->minPropertyPath) {
            $violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath);
        }
        $violationBuilder->addViolation();
        return;
    }
    if ($hasUpperLimit && $value > $max) {
        $violationBuilder = $this->context
            ->buildViolation($constraint->maxMessage)
            ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
            ->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
            ->setCode(Range::TOO_HIGH_ERROR);
        if (null !== $constraint->maxPropertyPath) {
            $violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath);
        }
        if (null !== $constraint->minPropertyPath) {
            $violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath);
        }
        $violationBuilder->addViolation();
        return;
    }
    if ($hasLowerLimit && $value < $min) {
        $violationBuilder = $this->context
            ->buildViolation($constraint->minMessage)
            ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
            ->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
            ->setCode(Range::TOO_LOW_ERROR);
        if (null !== $constraint->maxPropertyPath) {
            $violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath);
        }
        if (null !== $constraint->minPropertyPath) {
            $violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath);
        }
        $violationBuilder->addViolation();
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal