public function AccountFormController::validate

Overrides Drupal\Core\Entity\EntityFormController::submit().

Overrides EntityFormController::validate

File

drupal/core/modules/user/lib/Drupal/user/AccountFormController.php, line 248
Definition of Drupal\user\AccountFormController.

Class

AccountFormController
Form controller for the user account forms.

Namespace

Drupal\user

Code

public function validate(array $form, array &$form_state) {
  parent::validate($form, $form_state);
  $account = $this->entity;

  // Validate new or changing username.
  if (isset($form_state['values']['name'])) {
    if ($error = user_validate_name($form_state['values']['name'])) {
      form_set_error('name', $error);
    }
    else {
      $name_taken = (bool) db_select('users')
        ->fields('users', array(
        'uid',
      ))
        ->condition('uid', (int) $account->uid, '<>')
        ->condition('name', db_like($form_state['values']['name']), 'LIKE')
        ->range(0, 1)
        ->execute()
        ->fetchField();
      if ($name_taken) {
        form_set_error('name', t('The name %name is already taken.', array(
          '%name' => $form_state['values']['name'],
        )));
      }
    }
  }
  $mail = $form_state['values']['mail'];
  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 e-mail address %email is already taken.', array(
          '%email' => $mail,
        )));
      }
      else {
        form_set_error('mail', t('The e-mail 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 = drupal_get_schema('users');
    if (drupal_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'],
      )));
    }
  }
}