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

Breadcrumb

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

class DateValidator

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
    • class \Symfony\Component\Validator\Constraints\DateValidator extends \Symfony\Component\Validator\ConstraintValidator

Expanded class hierarchy of DateValidator

File

vendor/symfony/validator/Constraints/DateValidator.php, line 22

Namespace

Symfony\Component\Validator\Constraints
View source
class DateValidator extends ConstraintValidator {
    public const PATTERN = '/^(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})$/D';
    
    /**
     * Checks whether a date is valid.
     *
     * @internal
     */
    public static function checkDate(int $year, int $month, int $day) : bool {
        return checkdate($month, $day, $year);
    }
    public function validate(mixed $value, Constraint $constraint) : void {
        if (!$constraint instanceof Date) {
            throw new UnexpectedTypeException($constraint, Date::class);
        }
        if (null === $value || '' === $value) {
            return;
        }
        if (!\is_scalar($value) && !$value instanceof \Stringable) {
            throw new UnexpectedValueException($value, 'string');
        }
        $value = (string) $value;
        if (!preg_match(static::PATTERN, $value, $matches)) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Date::INVALID_FORMAT_ERROR)
                ->addViolation();
            return;
        }
        if (!self::checkDate($matches['year'] ?? $matches[1], $matches['month'] ?? $matches[2], $matches['day'] ?? $matches[3])) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Date::INVALID_DATE_ERROR)
                ->addViolation();
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConstraintValidator::$context protected property
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
ConstraintValidator::OBJECT_TO_STRING public constant Whether to cast objects with a &quot;__toString()&quot; method to strings.
ConstraintValidator::PRETTY_DATE public constant Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
(if it is available) or as RFC-3339 dates (&quot;Y-m-d H:i:s&quot;).
DateValidator::checkDate public static function Checks whether a date is valid.
DateValidator::PATTERN public constant
DateValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate 1
RSS feed
Powered by Drupal