function webform_validate_time in Webform 7.4
Same name and namespace in other branches
- 5.2 components/time.inc \webform_validate_time()
- 5 components/time.inc \webform_validate_time()
- 6.3 components/time.inc \webform_validate_time()
- 6.2 components/time.inc \webform_validate_time()
- 7.3 components/time.inc \webform_validate_time()
Validate that the time data is valid, calling form_error() if not.
1 string reference to 'webform_validate_time'
- _webform_render_time in components/
time.inc - Implements _webform_render_component().
File
- components/
time.inc, line 309 - Webform module time component.
Code
function webform_validate_time($element, $form_state) {
// Check if the user filled the required fields.
if ($element['#required']) {
foreach (array(
'hour',
'minute',
'ampm',
) as $field_type) {
if (isset($element[$field_type]) && $element[$field_type]['#value'] === '') {
form_error($element, t('!name field is required.', array(
'!name' => $element['#title'],
)));
return;
}
}
}
// Check for a valid time. Allow a minute with no hour as "no time set".
if ($element['hour']['#value'] !== '') {
if (!is_numeric($element['hour']['#value']) || !is_numeric($element['minute']['#value']) || isset($element['ampm']) && $element['ampm']['#value'] === '') {
form_error($element, t('Entered !name is not a valid time.', array(
'!name' => $element['#title'],
)));
return;
}
// Enforce the start and end times, if any.
$timestamp = strtotime($element['hour']['#value'] . ':' . $element['minute']['#value'] . ' ' . (isset($element['ampm']) ? $element['ampm']['#value'] : ''));
$start_time = strtotime($element['#start_time']);
$end_time = strtotime($element['#end_time']);
$subs = array(
'@start_time' => $element['#start_time'],
'@end_time' => $element['#end_time'],
);
if ($start_time !== FALSE && $end_time !== FALSE && $start_time > $end_time) {
// Validate as "over midnight" date range.
if ($end_time < $timestamp && $timestamp < $start_time) {
form_error($element, t('The entered time must be from @start_time to midnight to @end_time.', $subs));
}
}
else {
// Validate the start and end times are a regular (over noon) time range.
if ($start_time !== FALSE && $timestamp < $start_time) {
form_error($element, t('The entered time must be no earlier than @start_time.', $subs));
}
if ($end_time !== FALSE && $timestamp > $end_time) {
form_error($element, t('The entered time must be no later than @end_time.', $subs));
}
}
}
}