class UserNameConstraintValidator
Validates the UserName constraint.
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Drupal\user\Plugin\Validation\Constraint\UserNameConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of UserNameConstraintValidator
File
-
core/
modules/ user/ src/ Plugin/ Validation/ Constraint/ UserNameConstraintValidator.php, line 13
Namespace
Drupal\user\Plugin\Validation\ConstraintView source
class UserNameConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) : void {
if (empty($items) || $items instanceof FieldItemListInterface && $items->isEmpty()) {
$this->context
->addViolation($constraint->emptyMessage);
return;
}
$name = $items instanceof FieldItemListInterface ? $items->first()->value : $items;
if (str_starts_with($name, ' ')) {
$this->context
->addViolation($constraint->spaceBeginMessage);
}
if (str_ends_with($name, ' ')) {
$this->context
->addViolation($constraint->spaceEndMessage);
}
if (str_contains($name, ' ')) {
$this->context
->addViolation($constraint->multipleSpacesMessage);
}
if (preg_match('/[^\\x{80}-\\x{F7} a-z0-9@+_.\'-]/i', $name) || preg_match('/[\\x{80}-\\x{A0}' . '\\x{AD}' . '\\x{2000}-\\x{200F}' . '\\x{2028}-\\x{202F}' . '\\x{205F}-\\x{206F}' . '\\x{FEFF}' . '\\x{FF01}-\\x{FF60}' . '\\x{FFF9}-\\x{FFFD}' . '\\x{0}-\\x{1F}]/u', $name)) {
$this->context
->addViolation($constraint->illegalMessage);
}
if (mb_strlen($name) > UserInterface::USERNAME_MAX_LENGTH) {
$this->context
->addViolation($constraint->tooLongMessage, [
'%name' => $name,
'%max' => UserInterface::USERNAME_MAX_LENGTH,
]);
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
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 "__toString()" 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 ("Y-m-d H:i:s"). |
|
UserNameConstraintValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |