1

I am using this code to create user.

  if(user_validate_name('czxc') != ""){
        $user = User::create([
                'name' =>'czxc',    
                'mail' => 'user@gmail.comgczxcdfa',
                'pass' => 'password',
                'status' => 1
             ]);
            $user->save();
            drupal_set_message($user->id());

    }else{
        drupal_set_message('Already exist user'); 
    }
   This code works only one time for me.
2

The user_validate_name function from Drupal only verifies the syntax of the given name.

It does not validate whether the username exists or not.

You could replace the function call, like so:

$uids = \Drupal::entityQuery('user')
  ->condition('name', 'czxc')
  ->execute();
if (count($uids) > 0) {
  $user = User::create([
    'name' =>'czxc',
    'mail' => 'user@gmail.comgczxcdfa',
    'pass' => 'password',
    'status' => 1
  ]);
  $user->save();
  drupal_set_message($user->id());

} else {
  drupal_set_message('Already exist user');
}
  • Could entityTypeManager service be used to loadByProperties the account name? – Kevin Oct 3 '18 at 16:02
  • 1
    @Kevin Same thing as I mentioned on the other answer - probably no point loading the full entity when you just need to know if it exists – Clive Oct 3 '18 at 16:03
  • @Kevin, yes, loadByProperties loads the whole user entity, then you can get the name. – Cesar Moore Oct 10 '18 at 23:10
-1

user_validate_name() just verifies that:

  • The username isn't empty
  • The username doesn't begin with a space
  • The username doesn't end with a space
  • The username doesn't contain multiple spaces in row
  • The username contains only allowed characters
  • The username isn't too long

user_validate_name() isn't helpful to verify the username has not been already used, but slightly modifying its code can help in achieve the result you want.

  $definition = BaseFieldDefinition::create('string')->addConstraint('UserNameUnique', []);
  $data = \Drupal::typedDataManager()->create($definition);
  $data->setValue('czxc');
  $violations = $data->validate();
  if (count($violations) == 0) {
    // The username is not used from another account.
    $user = User::create([
      'name' =>'czxc',    
      'mail' => 'user@gmail.comgczxcdfa',
      'pass' => 'password',
      'status' => 1
    ]);
    $user->save();
  }

I would also verify the email has not been already used from another account, since in Drupal emails are required to be unique.

  $definition = BaseFieldDefinition::create('string')->addConstraint('UserNameUnique', []);
  $data = \Drupal::typedDataManager()->create($definition);
  $data->setValue('czxc');
  $name_violations = $data->validate();
  $definition = BaseFieldDefinition::create('string')->addConstraint('UserMailUnique', []);
  $data = \Drupal::typedDataManager()->create($definition);
  $data->setValue('user@gmail.comgczxcdfa');
  $email_violations = $data->validate();
  if (count($name_violations) == 0 && count($email_violations) == 0) {
    // The username and email aren't used from another account.
    $user = User::create([
      'name' =>'czxc',    
      'mail' => 'user@gmail.comgczxcdfa',
      'pass' => 'password',
      'status' => 1
    ]);
    $user->save();
  }

Not the answer you're looking for? Browse other questions tagged or ask your own question.