You are here

function advpoll_validate in Advanced Poll 6.3

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

Implementation of hook_validate().

Validate the editing of an advpoll node.

File

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

Code

function advpoll_validate($node, &$form) {

  // Use form_set_error for any errors.
  $node->choice = array_values($node->choice);

  // TODO: verify if this hack is still needed in Drupal 6.
  // Start keys at 1 rather than 0.
  array_unshift($node->choice, '');
  unset($node->choice[0]);

  // Check for at least two choices.
  $real_choices = 0;

  // TODO: take out _POST
  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.'));
  }
}