You are here

function invite_form_validate in Invite 7.2

Same name and namespace in other branches
  1. 5.2 invite.module \invite_form_validate()
  2. 5 invite.module \invite_form_validate()
  3. 6.2 invite.module \invite_form_validate()
  4. 7.4 includes/invite.admin.inc \invite_form_validate()

Forms API callback; validate submitted form data.

Filters out e-mails that are already registered or have been invited before. Checks the invite limit of the user and the max. number of invites per turn.

File

./invite.module, line 1190
Allows your users to send and track invitations to join your site.

Code

function invite_form_validate($form, &$form_state) {
  global $user;
  $emails = _invite_get_emails($form_state['values']['email']);
  if (!$form_state['values']['reg_code']) {
    if (count($emails) > 0) {

      // Filter out already registered users, but pass validation.
      $failed_emails = _invite_filter_registered_emails($emails);
      $emails = array_diff($emails, $failed_emails);
      if (count($failed_emails)) {
        $error = format_plural(count($failed_emails), 'The following recipient is already a member:', 'The following recipients are already members:') . '<br />';
        foreach ($failed_emails as $key => $email) {
          $failed_emails[$key] = check_plain($email);
        }
        $error .= implode(', ', $failed_emails);
        drupal_set_message($error, 'error');
      }
    }
    if (!empty($emails)) {

      // Filter out already invited users, but pass validation.
      $failed_emails = _invite_filter_invited_emails($emails);
      $emails = array_diff($emails, $failed_emails);
      if (count($failed_emails)) {
        $error = format_plural(count($failed_emails), 'The following recipient has already been invited:', 'The following recipients have already been invited:') . '<br />';
        $error .= implode(', ', array_map('check_plain', $failed_emails));
        drupal_set_message($error, 'error');
      }
    }

    // Check that there is at least one valid e-mail remaining after filtering
    // out dupes.
    if (count($emails) == 0) {
      form_set_error('email');
      return;
    }

    // Check invite limit, fail to let the user choose which ones to send.
    if (isset($form_state['values']['remaining_invites']) && count($emails) > $form_state['values']['remaining_invites']) {
      form_set_error('email', format_plural($form_state['values']['remaining_invites'], 'You have only 1 invite left.', 'You have only @count invites left.'));
      return;
    }

    // Check number of e-mails.
    if (!user_access('send mass invitations') && count($emails) > 1) {
      form_set_error('email', t('You cannot send more than one invitation.'));
      return;
    }
  }

  // Save valid emails.
  $form_state['values']['valid_emails'] = $emails;
}