You are here

function datelist_validate in Date 8

Validates the date type to adjust 12 hour time and prevent invalid dates (e.g., February 30, 2006).

If the date is valid, the date is set in the form as a string using the format designated in __toString().

1 string reference to 'datelist_validate'
_date_api_element_info in date_api/date_api_elements.inc
Implements hook_element_info().

File

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

Code

function datelist_validate($element, &$form_state) {
  $input_exists = FALSE;
  $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  if ($input_exists) {
    $date = datelist_get_input_date($element, $input);
    if ($date instanceof \Drupal\Core\Datetime\DrupalDateTime && !$date
      ->hasErrors()) {
      form_set_value($element, $date
        ->__toString(), $form_state);
    }
    elseif (!$element['#required']) {
      form_set_value($element, NULL, $form_state);
    }
    else {
      form_error($element, t('The specified date is invalid.'));
    }
  }
}