You are here

function node_recur_node_recur_form_validate in Node recur 7

Same name and namespace in other branches
  1. 7.2 node_recur.pages.inc \node_recur_node_recur_form_validate()

Validate the node recur form

1 call to node_recur_node_recur_form_validate()
node_recur_node_form_validate in ./node_recur.module
Validation handler for the node recur form on the node form

File

./node_recur.pages.inc, line 122

Code

function node_recur_node_recur_form_validate(&$form, &$form_state) {

  // If this is the confirm form, then skip validation
  if (isset($form_state['values']['confirm'])) {
    return;
  }

  // If option is set to nothing, then skip validation
  if ($form_state['values']['option'] == 'none') {
    return;
  }
  $node = $form['#node'];

  // If days option is selected, make sure we have at least one day
  if ($form_state['values']['option'] == 'days') {
    $days = 0;
    foreach ($form_state['values']['days'] as $day => $value) {
      if ($value) {
        $days++;
      }
    }
    if ($days == 0) {
      form_set_error('days', t('At least one day must be selected.'));
    }
  }

  // Check until date format
  $until = strtotime($form_state['values']['until']);
  if (!is_numeric($until)) {
    form_set_error('until', t('You must supply a valid end date.'));

    // Stop here
    return;
  }

  // Make sure until date is in the future
  if (!node_recur_allow_past_dates($node->type) && $until < REQUEST_TIME) {
    form_set_error('until', t('The end date must be in the future.'));
  }

  // Check that until date isn't too far in the future, according
  // to node settings (if any)
  if ($max = node_recur_max_future_date_span($node->type)) {
    if ($until > strtotime($max, REQUEST_TIME)) {
      form_set_error('until', t('The end date can only be up to %max in the future.', array(
        '%max' => $max,
      )));
    }
  }

  // If there were no errors, allow other modules to validate the dates
  if (!form_get_errors()) {
    $errors = module_invoke_all('node_recur_validate_dates', $node, $form_state);
    foreach ($errors as $error) {
      form_set_error($error['field'], $error['message']);
    }
  }
}