You are here

function user_validate_name in Drupal 8

Same name and namespace in other branches
  1. 4 modules/user.module \user_validate_name()
  2. 5 modules/user/user.module \user_validate_name()
  3. 6 modules/user/user.module \user_validate_name()
  4. 7 modules/user/user.module \user_validate_name()
  5. 9 core/modules/user/user.module \user_validate_name()

Verify the syntax of the given name.

Parameters

string $name: The user name to validate.

Return value

string|null A translated violation message if the name is invalid or NULL if the name is valid.

2 calls to user_validate_name()
SiteConfigureForm::validateForm in core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php
Form validation handler.
UserValidationTest::testUsernames in core/modules/user/tests/src/Kernel/UserValidationTest.php
Tests user name validation.

File

core/modules/user/user.module, line 301
Enables the user registration and login system.

Code

function user_validate_name($name) {
  $definition = BaseFieldDefinition::create('string')
    ->addConstraint('UserName', []);
  $data = \Drupal::typedDataManager()
    ->create($definition);
  $data
    ->setValue($name);
  $violations = $data
    ->validate();
  if (count($violations) > 0) {
    return $violations[0]
      ->getMessage();
  }
}