You are here

function registration_form_validate in Entity Registration 8

Same name and namespace in other branches
  1. 8.2 includes/registration.forms.inc \registration_form_validate()
  2. 7.2 includes/registration.forms.inc \registration_form_validate()
  3. 7 includes/registration.forms.inc \registration_form_validate()

Validation callback for registration_form().

File

includes/registration.forms.inc, line 170
Form definitions and callbacks for Registration.

Code

function registration_form_validate($form, &$form_state) {
  $registration = $form_state['registration'];
  $count = $form_state['values']['count'];
  $label = entity_load_single($registration->entity_type, $registration->entity_id)
    ->label();

  // Test status on new registrations.
  if (isset($registration->is_new) && $registration->is_new) {
    $errors = array();
    $registration_status = registration_status($registration->entity_type, $registration->entity_id, TRUE, $count, $registration->registration_id, $errors);
    if (!$registration_status) {
      form_set_error('', t('Sorry, unable to register for %label due to: %errors.', array(
        '%label' => $label,
        '%errors' => implode(', ', $errors),
      )));
    }
  }
  elseif (in_array($registration->state, registration_get_active_states())) {
    $has_room = registration_has_room($registration->entity_type, $registration->entity_id, $count, $registration->registration_id);
    if (!$has_room) {
      form_set_error('', t('Sorry, unable to register for %label due to: insufficient spaces remaining.', array(
        '%label' => $label,
      )));
    }
  }
  $settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
  $allow_multiple = !empty($settings['settings']['multiple_registrations']) && $settings['settings']['multiple_registrations'];

  // Validate according to who's registering.
  switch ($form_state['values']['who_is_registering']) {
    case REGISTRATION_REGISTRANT_TYPE_ANON:
      $form_state['values']['anon_mail'] = trim($form_state['values']['anon_mail']);
      if (!valid_email_address($form_state['values']['anon_mail'])) {
        form_set_error('anon_mail', t('The email address is invalid.'));
      }
      if (!$allow_multiple && registration_is_registered($registration, $form_state['values']['anon_mail'])) {
        form_set_error('anon_mail', t('%mail is already registered for this event.', array(
          '%mail' => $form_state['values']['anon_mail'],
        )));
      }
      break;
    case REGISTRATION_REGISTRANT_TYPE_ME:
      $user = \Drupal::currentUser();
      if (!$allow_multiple && registration_is_registered($registration, NULL, $user->uid)) {
        form_set_error('user', t('You are already registered for this event.'));
      }
      break;
    case REGISTRATION_REGISTRANT_TYPE_USER:
      $user = user_load_by_name($form_state['values']['user']);
      if ($user) {
        if (!$allow_multiple && registration_is_registered($registration, NULL, $user->uid)) {
          form_set_error('user', t('%user is already registered for this event.', array(
            '%user' => $user->name,
          )));
        }
      }
      else {
        form_set_error('user', t('%user is not a valid user.', array(
          '%user' => $form_state['values']['user'],
        )));
      }
      break;
  }

  // Notify field widgets to validate their data.
  field_attach_form_validate('registration', $registration, $form, $form_state);
}