function UriHostConstraintValidator::isValid

Return TRUE if value is a valid hostname or IP address literal.

1 call to UriHostConstraintValidator::isValid()
UriHostConstraintValidator::validate in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UriHostConstraintValidator.php

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UriHostConstraintValidator.php, line 38

Class

UriHostConstraintValidator
Validates if a string conforms to the RFC 3986 host component.

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

Code

protected function isValid(string $value) : bool {
    if (filter_var($value, \FILTER_VALIDATE_DOMAIN, \FILTER_FLAG_HOSTNAME) !== FALSE) {
        return TRUE;
    }
    if (str_starts_with($value, '[') && str_ends_with($value, ']')) {
        $address = substr($value, 1, strlen($value) - 2);
        if (filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== FALSE) {
            return TRUE;
        }
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.