You are here

function date_select_validate in Date 6

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. 7.3 date_api/date_api_elements.inc \date_select_validate()
  4. 7 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.

1 string reference to 'date_select_validate'
_date_api_elements in ./date_api_elements.inc
Implementation of hook_elements().

File

./date_api_elements.inc, line 358
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 (empty($element['#value'])) {
    $element['#value'] = array();
  }
  $error_field = implode('][', $element['#parents']);
  $granularity = array_values(date_format_order($element['#date_format']));

  // Strip field labels out of the results.
  foreach ($element['#value'] as $field => $value) {
    if (substr($value, 0, 1) == '-') {
      $element['#value'][$field] = '';
    }
  }
  if (isset($element['#value']['ampm'])) {
    if ($element['#value']['ampm'] == 'pm' && $element['#value']['hour'] < 12) {
      $element['#value']['hour'] += 12;
    }
    elseif ($element['#value']['ampm'] == 'am' && $element['#value']['hour'] == 12) {
      $element['#value']['hour'] -= 12;
    }
  }
  if ($element['#required'] || !empty($form_values['year'])) {
    if ($element['#value']['year'] < variable_get('date_min_year', 1) || $element['#value']['year'] > variable_get('date_max_year', 4000)) {
      form_set_error($error_field . '][year', t('year must be a number between %min and %max.', array(
        '%min' => variable_get('date_min_year', 1),
        '%max' => variable_get('date_max_year', 4000),
      )));
    }
  }
  if (in_array('month', $granularity) && ($element['#required'] || !empty($element['#value']['month']))) {
    if ($element['#value']['month'] < 1 || $element['#value']['month'] > 12) {
      form_set_error($error_field . '][month', t('month must be a number between 1 and 12.'));
    }
  }
  if (in_array('day', $granularity) && ($element['#required'] || !empty($element['#value']['day']))) {
    if ($element['#value']['day'] < 1 || $element['#value']['day'] > 31) {
      form_set_error($error_field . '][day', t('day must be a number between 1 and 31.'));
    }
  }
  if (!form_get_errors()) {

    // If it creates a valid date, set it.
    $value = date_select_input_value($element);
    if (!empty($value)) {
      form_set_value($element, $value, $form_state);
    }
    elseif ($element['#required']) {
      form_set_error($error_field, t('A valid date cannot be constructed from %m-%d-%y.', array(
        '%m' => $element['#value']['month'],
        '%d' => $element['#value']['day'],
        '%y' => $element['#value']['year'],
      )));
    }
    else {
      form_set_value($element, NULL, $form_state);
    }
  }
}