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

Breadcrumb

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

function HostnameValidator::validate

Overrides ConstraintValidatorInterface::validate

File

vendor/symfony/validator/Constraints/HostnameValidator.php, line 34

Class

HostnameValidator
@author Dmitrii Poddubnyi <dpoddubny@gmail.com>

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate(mixed $value, Constraint $constraint) : void {
    if (!$constraint instanceof Hostname) {
        throw new UnexpectedTypeException($constraint, Hostname::class);
    }
    if (null === $value || '' === $value) {
        return;
    }
    if (!\is_scalar($value) && !$value instanceof \Stringable) {
        throw new UnexpectedValueException($value, 'string');
    }
    $value = (string) $value;
    if ('' === $value) {
        return;
    }
    if (!$this->isValid($value) || $constraint->requireTld && !$this->hasValidTld($value)) {
        $this->context
            ->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode(Hostname::INVALID_HOSTNAME_ERROR)
            ->addViolation();
    }
}

API Navigation

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