1 form.inc | html_time_validate($element, &$form_state) |
Validate a time string.
Related topics
File
- core/
includes/ form.inc, line 3361 - Functions for form and batch generation and processing.
Code
function html_time_validate($element, &$form_state) {
$time = $element['#value'];
if (!empty($time)) {
// Pad time if seconds were omitted because of the step attribute,
// or a browser skipped it. Our date pattern contains seconds.
if (preg_match('/^\d{2}:\d{2}$/', $time)) {
$time .= ':00';
}
$valid_time = FALSE;
$created_time = DateTime::createFromFormat(DATE_FORMAT_TIME, $time);
if (is_object($created_time)) {
// DateTime::createFromFormat alone is too permissive.
if ($created_time->format(DATE_FORMAT_TIME) == $time) {
$valid_time = TRUE;
}
}
if (!$valid_time) {
form_error($element, t('%time is not a valid time.', array('%time' => $time)));
}
}
}