You are here

function security_questions_user_answer_form_validate in Security Questions 6

Same name and namespace in other branches
  1. 7 security_questions.module \security_questions_user_answer_form_validate()

Validation handler for answer form.

2 string references to 'security_questions_user_answer_form_validate'
security_questions_form_user_login_alter in ./security_questions.module
Implements hook_form_FORM_ID_alter() for user_login().
security_questions_form_user_register_alter in ./security_questions.module
Implements hook_form_FORM_ID_alter() for user_register().

File

./security_questions.module, line 589
Main module file for security_questions.

Code

function security_questions_user_answer_form_validate($form, &$form_state) {

  // If we are in the login form, the account will be available
  // in the form_state because of our login name validation.
  if (isset($form_state['security_questions']['account'])) {
    $account = $form_state['security_questions']['account'];
  }
  else {
    global $user;
    $account = $user;
  }

  // Get a list of questions that the user has already answered.
  $answered = db_fetch_array(db_query('SELECT security_question_id FROM {security_questions_answers}
                        WHERE uid = %d', $account->uid));

  // Get the required number of questions and set counter.
  $required = security_questions_required_for_user($account);
  $i = 1;
  while ($i <= $required) {

    // Add newly answered questions to the $answered array if they are not
    // user supplied questions.
    if ($form_state['values']['security_question_id_' . $i] != 'other') {
      $answered[] = $form_state['values']['security_question_id_' . $i];
    }

    // Validate security questions answers are not empty
    if (empty($form_state['values']['security_question_user_answer_' . $i])) {
      form_set_error('security_question_user_answer_' . $i, t('Please enter a value for Answer !number.', array(
        '!number' => $i,
      )));
    }
    $i++;
  }
  if ($answered) {

    // Get an array of questions that have been answered more than once.
    $dupes = array_diff_key($answered, array_unique($answered));
    foreach ($dupes as $dupe) {
      form_set_error('security_question_id_' . $dupe, t('Please select a question that you have not yet picked.'));
    }
  }

  // If there were errors, logout user
  if (form_get_errors()) {

    // Load the anonymous user
    $user = drupal_anonymous_user();
  }
}