function RoleExistsConstraintValidator::validate

File

core/modules/user/src/Plugin/Validation/Constraint/RoleExistsConstraintValidator.php, line 39

Class

RoleExistsConstraintValidator
Validates that a role exists.

Namespace

Drupal\user\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) : void {
    assert($constraint instanceof RoleExistsConstraint);
    if (!is_string($value)) {
        throw new UnexpectedTypeException($value, 'string');
    }
    $roleStorage = $this->entity_type_manager
        ->getStorage('user_role');
    if (!$roleStorage->load($value)) {
        $this->context
            ->addViolation($constraint->message, [
            '@rid' => $value,
        ]);
    }
}

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