You are here

function date_select_validate in Date 7

Same name and namespace in other branches
  1. 5.2 date_api_elements.inc \date_select_validate()
  2. 6.2 date_api_elements.inc \date_select_validate()
  3. 6 date_api_elements.inc \date_select_validate()
  4. 7.3 date_api/date_api_elements.inc \date_select_validate()
  5. 7.2 date_api/date_api_elements.inc \date_select_validate()

Validation function for date selector.

When used as a Views widget, the validation step always gets triggered, even with no form submission. Before form submission $element['#value'] contains a string, after submission it contains an array.

1 string reference to 'date_select_validate'
date_select_element_process in date_api/date_api_elements.inc
Flexible date/time drop-down selector.

File

date_api/date_api_elements.inc, line 516
Date API elements themes and validation. This file is only included during the edit process to reduce memory usage.

Code

function date_select_validate($element, &$form_state) {
  if (is_string($element['#value'])) {
    return;
  }
  $input_exists = NULL;
  $input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists);

  // Strip field labels out of the results.
  foreach ($element['#value'] as $field => $field_value) {
    if (substr($field_value, 0, 1) == '-') {
      $input[$field] = '';
    }
  }
  $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  if (isset($element['#value']['ampm'])) {
    if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
      $input['hour'] += 12;
    }
    elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
      $input['hour'] -= 12;
    }
  }
  unset($input['ampm']);
  $date = date_select_input_date($element, $input);
  if (empty($date) && $element['#required']) {
    form_error($element, t('A valid value is required.'));
  }
  elseif (!empty($date->errors)) {
    form_error($element, implode('<br />', $date->errors));
  }
  elseif (!empty($date)) {
    form_set_value($element, $date
      ->format(DATE_FORMAT_DATETIME), $form_state);
  }
  else {
    form_set_value($element, NULL, $form_state);
  }
}