You are here

function node_registration_form_validate in Node registration 7

Validation handler for node_registration_form().

1 string reference to 'node_registration_form_validate'
node_registration_form in includes/node_registration.forms.inc
The registration form. It takes a (mandatory) existing or empty registration object.

File

includes/node_registration.forms.inc, line 927
New registration forms. Public and admin.

Code

function node_registration_form_validate($form, &$form_state) {
  $registration = $form['#registration'];
  $is_new = (bool) $registration->is_new;
  $node = $form['#node'];
  $account = $form['#account'];
  $settings = $node->registration;
  $admin = user_access('administer node registration');
  $mail_admin = node_registration_node_access($node, 'administer');
  $can_register_others = node_registration_node_access($node, 'register others');
  $values =& $form_state['values'];

  // Verify the event hasn't sold out.
  if ($is_new && !_node_registration_event_has_room($node) && !$settings->allow_exceeding_capacity) {
    return form_set_error('email', t("Sorry, this event's capacity during your registration."));
  }

  // Change user context for admins.
  if ($can_register_others && isset($form['account'])) {
    $account = '' == $values['account'] ? drupal_anonymous_user() : user_load_by_name($values['account']);
  }

  // E-mail not editable.
  if (isset($form['#fixed_email'])) {
    $values['email'] = $form['#fixed_email'];
  }
  if (empty($values['email'])) {
    if ($account->uid) {
      $values['email'] = $account->mail;
    }
    else {
      return form_set_error('email', t("You have to submit an e-mail address for anonymous registrations."));
    }
  }
  else {
    $existing_account = user_load_by_mail($values['email']);
    if ($existing_account && $existing_account->uid != $account->uid && !$mail_admin) {
      return form_set_error('email', t('This e-mail address belongs to a registered user. If you are that user, please <a href="!url">log in</a> first.', array(
        '!url' => url('user/login'),
      )));
    }
  }

  // Used values / flags.
  $email = $values['email'];

  // Notify field widgets to validate their data.
  _node_registration_fields_callback('field_attach_form_validate', $registration, $form, $form_state);
  $errors = form_get_errors();
  if (!$errors) {

    // E-mail address must be unique per event.
    $registered = _node_registration_email_registered($node, $email);
    if ($registered) {
      if (!$registration->registration_id || $registration->registration_id != $registered->registration_id) {
        form_set_error('email', t('%email is already registered for this event.', array(
          '%email' => $email,
        )));
      }
    }

    // Alter registration object and collect custom errors.
    $context = array(
      'form' => $form,
      'form_state' => $form_state,
      'node' => $node,
      'user' => $account,
    );
    $errors = array();
    drupal_alter('node_registration_submit', $registration, $context, $errors);

    // Set custom form errors.
    foreach ($errors as $field => $error) {
      form_set_error($field, $error);
    }
  }
}