You are here

function advpoll_validate in Advanced Poll 5

Same name and namespace in other branches
  1. 6.3 advpoll.module \advpoll_validate()
  2. 6 advpoll.module \advpoll_validate()
  3. 6.2 advpoll.module \advpoll_validate()

Implementation of hook_validate().

File

./advpoll.module, line 656
Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.

Code

function advpoll_validate($node, &$form) {
  global $form_values;
  if ($form_values['op'] == t('Reset votes')) {
    drupal_goto('node/' . $node->nid . '/reset');
  }
  else {

    // Check for at least two choices.
    if (isset($node->choice_area)) {

      // Handle a textarea rather than separate fields per choice.
      $real_choices = count(explode("\n", $node->choice_area));
    }
    else {
      $node->choice = array_values($node->choice);

      // Start keys at 1 rather than 0.
      array_unshift($node->choice, '');
      unset($node->choice[0]);
      $real_choices = 0;
      foreach ($_POST['choice'] as $i => $choice) {
        if ($choice['label'] != '') {
          $real_choices++;
        }
      }
    }
    if ($real_choices < 2) {
      form_set_error("choice][{$real_choices}][label", t('You must fill in at least two choices.'));
    }

    // Validate max choices since it has #DANGEROUS_SKIP_CHECK set to true.
    if ($node->settings['max_choices'] < 0) {
      form_set_error('settings][max_choices]', t('Maximum choices must be a non-negative integer.'));
    }
    if ($node->settings['max_choices'] > $real_choices) {
      form_set_error('settings][max_choices]', t('Maximum choices cannot be larger than the number of choices submitted.'));
    }

    // Validate dates.
    if (!empty($node->settings['start_date']) && strtotime($node->settings['start_date']) <= 0) {
      form_set_error('settings][start_date', t('You have to specify a valid starting date.'));
    }
    if (!empty($node->settings['end_date']) && strtotime($node->settings['end_date']) <= 0) {
      form_set_error('settings][end_date', t('You have to specify a valid ending date.'));
    }
    if (!empty($node->settings['end_date']) && $node->settings['end_date'] < $node->settings['start_date']) {
      form_set_error('settings][end_date', t('Ending date cannot be before the starting date.'));
    }
  }
}