You are here

function advpoll_validate_max_choices in Advanced Poll 7.3

Same name and namespace in other branches
  1. 7 advpoll.module \advpoll_validate_max_choices()
  2. 7.2 advpoll.module \advpoll_validate_max_choices()

Validate node fields.

Extra validation for advpoll node edit - max choices field. Make sure that the max choices for a poll never exceeds the number of choices entered by a user.

1 string reference to 'advpoll_validate_max_choices'
advpoll_form_alter in ./advpoll.module
Implements hook_form_alter().

File

./advpoll.module, line 393

Code

function advpoll_validate_max_choices($element, &$form_state, $form) {
  $input_choices = array();
  $lang = entity_language('node', $form_state['node']);
  if (!isset($form_state['input']['advpoll_choice'][$lang])) {
    $lang = LANGUAGE_NONE;
  }
  $choices = $form_state['values']['advpoll_choice'][$lang];
  if (array_key_exists('add_more', $choices)) {
    unset($choices['add_more']);
  }
  foreach ($choices as $choice) {
    if ($choice['choice']) {
      $input_choices[] = $choice['choice'];
    }
  }
  $max = (int) $element['#value'];
  if ($max > count($input_choices)) {
    form_error($element, t('The max number of choices is @max.', array(
      '@max' => $max,
    )));
  }
  if (count($input_choices) < 2) {
    form_set_error('advpoll_choice', t('Please enter at least two choices.'));
  }
}