1 form.inc html_date_validate($element, &$form_state)

Validate a date string.

Related topics

File

core/includes/form.inc, line 3299
Functions for form and batch generation and processing.

Code

function html_date_validate($element, &$form_state) {
  $date = $element['#value'];
  if (!empty($date)) {
    $valid_date = FALSE;
    $created_date = DateTime::createFromFormat(DATE_FORMAT_DATE, $date);
    if (is_object($created_date)) {
      // DateTime::createFromFormat alone is too permissive.
      if ($created_date->format(DATE_FORMAT_DATE) == $date) {
        $valid_date = TRUE;
      }
    }
    if (!$valid_date) {
      form_error($element, t('%date is not a valid date.', array('%date' => $date)));
    }
  }
}