You are here

function date_combo_validate in Date 7

Same name and namespace in other branches
  1. 8 date_elements.inc \date_combo_validate()
  2. 5.2 date/date_elements.inc \date_combo_validate()
  3. 6.2 date/date_elements.inc \date_combo_validate()
  4. 6 date/date_elements.inc \date_combo_validate()
  5. 7.3 date_elements.inc \date_combo_validate()
  6. 7.2 date_elements.inc \date_combo_validate()

Validate and update a combo element. Don't try this if there were errors before reaching this point.

2 string references to 'date_combo_validate'
date_element_info in ./date.module
Implements hook_element_info().
date_field_widget_form in ./date_elements.inc
Private implementation of hook_widget().

File

./date_elements.inc, line 319
Date forms and form themes and validation.

Code

function date_combo_validate($element, &$form_state) {
  $form_values = $form_state['values'];
  $field_name = $element['#field_name'];
  $delta = $element['#delta'];
  $langcode = $element['#language'];

  // If the whole field is empty and that's OK, stop now.
  if (empty($form_state['input'][$field_name]) && !$element['#required']) {
    return;
  }

  // Repeating dates have a different form structure, so get the
  // right item values.
  $item = isset($form_values[$field_name][$langcode]['rrule']) ? $form_values[$field_name][$langcode] : $form_values[$field_name][$langcode][$delta];
  $posted = isset($form_values[$field_name][$langcode]['rrule']) ? $form_state['input'][$field_name][$langcode] : $form_state['input'][$field_name][$langcode][$delta];
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);
  $from_field = 'value';
  $to_field = 'value2';
  $tz_field = 'timezone';
  $offset_field = 'offset';
  $offset_field2 = 'offset2';

  // Unfortunately, due to the fact that much of the processing is already
  // done by the time we get here, it is not possible highlight the field
  // with an error, we just try to explain which element is creating the
  // problem in the error message.
  $parent = $element['#parents'];
  $error_field = array_pop($parent);
  $errors = array();

  // Check for empty 'From date', which could either be an empty
  // value or an array of empty values, depending on the widget.
  $empty = TRUE;
  if (!empty($item[$from_field])) {
    if (!is_array($item[$from_field])) {
      $empty = FALSE;
    }
    else {
      foreach ($item[$from_field] as $key => $value) {
        if (!empty($value)) {
          $empty = FALSE;
          break;
        }
      }
    }
  }

  // A 'To' date without a 'From' date is a validation error.
  if ($empty && !empty($item[$to_field])) {
    if (!is_array($item[$to_field])) {
      form_set_error($error_field, t("A 'From date' date is required if a 'To date' is supplied for field %field #%delta.", array(
        '%delta' => $field['cardinality'] ? intval($delta + 1) : '',
        '%field' => t($instance['label']),
      )));
      $empty = FALSE;
    }
    else {
      foreach ($item[$to_field] as $key => $value) {
        if (!empty($value)) {
          form_set_error($error_field, t("A 'From date' date is required if a 'To date' is supplied for field %field #%delta.", array(
            '%delta' => $field['cardinality'] ? intval($delta + 1) : '',
            '%field' => t($instance['label']),
          )));
          $empty = FALSE;
          break;
        }
      }
    }
  }
  if ($empty) {
    $item = date_element_empty($element, $form_state);
    if (!$element['#required']) {
      return;
    }
  }
  elseif (!form_get_errors()) {

    // Check todate input for blank values and substitute in fromdate
    // values where needed, then re-compute the todate with those values.
    if ($field['settings']['todate']) {
      $merged_date = array();
      $to_date_empty = TRUE;
      foreach ($posted[$to_field] as $part => $value) {
        $to_date_empty = $to_date_empty && empty($value) && !is_numeric($value);
        $merged_date[$part] = empty($value) && !is_numeric($value) ? $posted[$from_field][$part] : $value;
        if ($part == 'ampm' && $merged_date['ampm'] == 'pm' && $merged_date['hour'] < 12) {
          $merged_date['hour'] += 12;
        }
        elseif ($part == 'ampm' && $merged_date['ampm'] == 'am' && $merged_date['hour'] == 12) {
          $merged_date['hour'] -= 12;
        }
      }

      // If all date values were empty and a date is required, throw
      // an error on the first element. We don't want to create
      // duplicate messages on every date part, so the error will
      // only go on the first.
      if ($to_date_empty && $field['settings']['todate'] == 'required') {
        $errors[] = t('Some value must be entered in the To date.');
      }
      $element[$to_field]['#value'] = $merged_date;

      // Call the right function to turn this altered user input into
      // a new value for the todate.
      $item[$to_field] = $merged_date;
    }
    else {
      $item[$to_field] = $item[$from_field];
    }
    $from_date = date_input_date($field, $instance, $element[$from_field], $posted[$from_field]);
    if (!empty($field['settings']['todate'])) {
      $to_date = date_input_date($field, $instance, $element[$to_field], $merged_date);
    }
    else {
      $to_date = $from_date;
    }

    // Neither the from date nor the to date should be empty at this point
    // unless they held values that couldn't be evaluated.
    if (!$instance['required'] && (empty($from_date) || empty($to_date))) {
      $item = date_element_empty($element, $form_state);
      $errors[] = t('The dates are invalid.');
    }
    elseif (!empty($field['settings']['todate']) && $from_date > $to_date) {
      form_set_value($element[$to_field], $to_date, $form_state);
      $errors[] = t('The To date must be greater than the From date.');
    }
    else {

      // Convert input dates back to their UTC values and re-format to ISO
      // or UNIX instead of the DATETIME format used in element processing.
      $timezone = !empty($item[$tz_field]) ? $item[$tz_field] : $element['#date_timezone'];
      $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
      $item[$tz_field] = $timezone;
      $item[$offset_field] = date_offset_get($from_date);
      $test_from = date_format($from_date, 'r');
      $test_to = date_format($to_date, 'r');
      $item[$offset_field2] = date_offset_get($to_date);
      date_timezone_set($from_date, timezone_open($timezone_db));
      date_timezone_set($to_date, timezone_open($timezone_db));
      $item[$from_field] = date_format($from_date, date_type_format($field['type']));
      $item[$to_field] = date_format($to_date, date_type_format($field['type']));
      if (isset($form_values[$field_name]['rrule'])) {
        $item['rrule'] = $form_values[$field['field_name']]['rrule'];
      }

      // Test a roundtrip back to the original timezone to catch
      // invalid dates, like 2AM on the day that spring daylight savings
      // time begins in the US.
      date_timezone_set($from_date, timezone_open($timezone));
      date_timezone_set($to_date, timezone_open($timezone));
      if ($test_from != date_format($from_date, 'r')) {
        $errors[] = t('The From date is invalid.');
      }
      if ($test_to != date_format($to_date, 'r')) {
        $errors[] = t('The To date is invalid.');
      }
      if (empty($errors)) {
        form_set_value($element, $item, $form_state);
      }
    }
  }
  if (!empty($errors)) {
    if ($field['cardinality']) {
      form_set_error($error_field, t('There are errors in @field_name value #@delta:', array(
        '@field_name' => $instance['label'],
        '@delta' => $delta + 1,
      )) . theme('item_list', array(
        'items' => $errors,
      )));
    }
    else {
      form_set_error($error_field, t('There are errors in @field_name:', array(
        '@field_name' => $instance['label'],
      )) . theme('item_list', array(
        'items' => $errors,
      )));
    }
  }
}