You are here

function security_questions_user_answer_form_validate in Security Questions 7

Same name and namespace in other branches
  1. 6 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_form_alter in ./security_questions.module
Implements hook_form_FORM_ID_alter() for user_register().

File

./security_questions.module, line 623
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_query('SELECT security_question_id FROM {security_questions_answers}
                        WHERE uid = :uid', array(
    ':uid' => $account->uid,
  ))
    ->fetchCol();

  // Get the required number of questions and set counter.
  $required = security_questions_required_for_user($account);
  $i = 1;
  while ($i <= $required) {
    if ($form_state['input']['security_question_id_' . $i] == 'other' && empty($form_state['input']['security_question_user_question_' . $i])) {
      form_set_error('security_question_user_question_' . $i, t('Please supply a question.'));
    }

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

  // 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.'));
  }
}