1 user.module user_account_form_validate($form, &$form_state)

Form validation handler for user_account_form().

See also

user_account_form()

File

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

Code

function user_account_form_validate($form, &$form_state) {
  $account = $form['#user'];
  $mail = $form_state['values']['mail'];

  // Validate new or changing username.
  if (isset($form_state['values']['name'])) {
    $name = $form_state['values']['name'];
    if ($error = user_validate_name($name)) {
      form_set_error('name', $error);
    }
    // Cast the user ID as an integer. It might have been set to NULL, which
    // could lead to unexpected results.
    else {
      $name_taken = (bool) db_select('users')
        ->fields('users', array('uid'))
        ->condition('uid', (int) $account->uid, '<>')
        ->condition('name', db_like($name), 'LIKE')
        ->range(0, 1)
        ->execute()
        ->fetchField();

      if ($name_taken) {
        form_set_error('name', t('The name %name is already taken.', array('%name' => $name)));
      }
      // Check whether the user name provided is an email address. If so, make
      // sure it matches the mail value.
      if (config('system.core')->get('user_email_match') && (valid_email_address($name))) {
        if ($name !== $mail) {
          form_set_error('name', t('An email address was provided as a username, but does not match the account email address.'));
        }
      }
    }
  }

  if (!empty($mail)) {
    $mail_taken = (bool) db_select('users')
      ->fields('users', array('uid'))
      ->condition('uid', (int) $account->uid, '<>')
      ->condition('mail', db_like($mail), 'LIKE')
      ->range(0, 1)
      ->execute()
      ->fetchField();

    if ($mail_taken) {
      // Format error message dependent on whether the user is logged in or not.
      if ($GLOBALS['user']->uid) {
        form_set_error('mail', t('The email address %email is already taken.', array('%email' => $mail)));
      }
      else {
        form_set_error('mail', t('The email address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $mail, '@password' => url('user/password'))));
      }
    }
  }

  // Make sure the signature isn't longer than the size of the database field.
  // Signatures are disabled by default, so make sure it exists first.
  if (isset($form_state['values']['signature'])) {
    // Move text format for user signature into 'signature_format'.
    $form_state['values']['signature_format'] = $form_state['values']['signature']['format'];
    // Move text value for user signature into 'signature'.
    $form_state['values']['signature'] = $form_state['values']['signature']['value'];

    $user_schema = backdrop_get_schema('users');
    if (backdrop_strlen($form_state['values']['signature']) > $user_schema['fields']['signature']['length']) {
      form_set_error('signature', t('The signature is too long: it must be %max characters or less.', array('%max' => $user_schema['fields']['signature']['length'])));
    }
  }
}