function webform_validate_date in Webform 7.4
Same name and namespace in other branches
- 5.2 components/date.inc \webform_validate_date()
- 5 components/date.inc \webform_validate_date()
- 6.3 components/date.inc \webform_validate_date()
- 6.2 components/date.inc \webform_validate_date()
- 7.3 components/date.inc \webform_validate_date()
Element validation for Webform date fields.
1 string reference to 'webform_validate_date'
- _webform_render_date in components/
date.inc - Implements _webform_render_component().
File
- components/
date.inc, line 409 - Webform module date component.
Code
function webform_validate_date($element, $form_state) {
$date_parts = array(
'day',
'month',
'year',
);
// Determine if the user has specified a date. Hidden parts of the date will
// be submitted automatically.
foreach ($date_parts as $date_part) {
if (!in_array($date_part, $element['#exclude']) && $element[$date_part]['#value'] !== '') {
$field_found = TRUE;
}
}
if (isset($field_found)) {
// Check that each part of the date has been filled in.
foreach ($date_parts as $date_part) {
if (empty($element[$date_part]['#value'])) {
form_error($element[$date_part], t('!part in !name is missing.', array(
'!name' => $element['#title'],
'!part' => $element[$date_part]['#title'],
)));
$missing_fields = TRUE;
}
}
if (isset($missing_fields)) {
return;
}
// Ensure date is made up of integers.
foreach ($date_parts as $date_part) {
$element[$date_part]['#value'] = (int) $element[$date_part]['#value'];
}
// Check for a valid date.
if (!checkdate($element['month']['#value'], $element['day']['#value'], $element['year']['#value'])) {
form_error($element, t('Entered !name is not a valid date.', array(
'!name' => $element['#title'],
)));
return;
}
// Create a timestamp of the entered value for comparison.
$timestamp = strtotime($element['year']['#value'] . '-' . $element['month']['#value'] . '-' . $element['day']['#value']);
$format = webform_date_format('short');
// Flip start and end if needed. Prior to 7.x-4.8, it was possible to save
// a date component with the end date earlier than the start date.
$date1 = strtotime($element['#start_date']);
$date2 = strtotime($element['#end_date']);
if ($date1 !== FALSE && $date2 !== FALSE) {
$start_date = $date1 < $date2 ? $date1 : $date2;
$end_date = $date1 > $date2 ? $date1 : $date2;
}
else {
$start_date = $date1;
$end_date = $date2;
}
// Check that the date is after the start date.
if ($start_date !== FALSE && $timestamp < $start_date) {
form_error($element, t('The entered date must be @start_date or later.', array(
'@start_date' => date($format, $start_date),
)));
}
// Check that the date is before the end date.
if ($end_date !== FALSE && $timestamp > $end_date) {
form_error($element, t('The entered date must be @end_date or earlier.', array(
'@end_date' => date($format, $end_date),
)));
}
}
elseif ($element['#required']) {
form_error($element, t('!name field is required.', array(
'!name' => $element['#title'],
)));
}
}