function date_combo_validate in Date 6.2
Same name and namespace in other branches
- 8 date_elements.inc \date_combo_validate()
- 5.2 date/date_elements.inc \date_combo_validate()
- 6 date/date_elements.inc \date_combo_validate()
- 7.3 date_elements.inc \date_combo_validate()
- 7 date_elements.inc \date_combo_validate()
- 7.2 date_elements.inc \date_combo_validate()
Validate and update a combo element. Don't try this if there were errors before reaching this point.
2 string references to 'date_combo_validate'
- _date_elements in date/
date_elements.inc - Implementation of hook_elements().
- _date_widget in date/
date_elements.inc - Private implementation of hook_widget().
File
- date/
date_elements.inc, line 378 - Date forms and form themes and validation.
Code
function date_combo_validate($element, &$form_state) {
$form_values = $form_state['values'];
$field_name = $element['#field_name'];
$delta = $element['#delta'];
// If the whole field is empty and that's OK, stop now.
if (empty($element['#post'][$field_name]) && !$element['#required']) {
return;
}
// Repeating dates have a different form structure, so get the
// right item values.
$item = isset($form_values[$field_name]['rrule']) ? $form_values[$field_name] : $form_values[$field_name][$delta];
$posted = isset($form_values[$field_name]['rrule']) ? $element['#post'][$field_name] : $element['#post'][$field_name][$delta];
$field = $form_state['#field_info'][$element['#field_name']];
$from_field = 'value';
$to_field = 'value2';
$tz_field = 'timezone';
$offset_field = 'offset';
$offset_field2 = 'offset2';
// Unfortunately, due to the fact that much of the processing is already
// done by the time we get here, it is not possible highlight the field
// with an error, we just try to explain which element is creating the
// problem in the error message.
$parent = $element['#parents'];
$error_field = array_pop($parent);
$errors = array();
// Check for empty 'From date', which could either be an empty
// value or an array of empty values, depending on the widget.
$empty = TRUE;
if (!empty($item[$from_field])) {
if (!is_array($item[$from_field])) {
$empty = FALSE;
}
else {
foreach ($item[$from_field] as $key => $value) {
if (!empty($value)) {
$empty = FALSE;
break;
}
}
}
}
if ($empty) {
$item = date_element_empty($element, $form_state);
if (!$element['#required']) {
return;
}
}
elseif (!form_get_errors()) {
// Check todate input for blank values and substitute in fromdate
// values where needed, then re-compute the todate with those values.
if ($field['todate']) {
$merged_date = array();
$to_date_empty = TRUE;
foreach ($posted[$to_field] as $part => $value) {
$to_date_empty = $to_date_empty && empty($value) && !is_numeric($value);
$merged_date[$part] = empty($value) && !is_numeric($value) ? $posted[$from_field][$part] : $value;
if ($part == 'ampm' && $merged_date['ampm'] == 'pm' && $merged_date['hour'] < 12) {
$merged_date['hour'] += 12;
}
elseif ($part == 'ampm' && $merged_date['ampm'] == 'am' && $merged_date['hour'] == 12) {
$merged_date['hour'] -= 12;
}
}
// If all date values were empty and a date is required, throw
// an error on the first element. We don't want to create
// duplicate messages on every date part, so the error will
// only go on the first.
if ($to_date_empty && $field['todate'] == 'required') {
$errors[] = t('Some value must be entered in the To date.');
}
$element[$to_field]['#value'] = $merged_date;
// Call the right function to turn this altered user input into
// a new value for the todate.
$item[$to_field] = $merged_date;
}
else {
$item[$to_field] = $item[$from_field];
}
$from_date = date_input_value($field, $element[$from_field]);
if (!empty($field['todate'])) {
$to_date = date_input_value($field, $element[$to_field]);
}
else {
$to_date = $from_date;
}
// Neither the from date nor the to date should be empty at this point
// unless they held values that couldn't be evaluated.
if (!$field['required'] && (empty($from_date) || empty($to_date))) {
$item = date_element_empty($element, $form_state);
$errors[] = t('The dates are invalid.');
}
elseif (!empty($field['todate']) && $from_date > $to_date) {
form_set_value($element[$to_field], $to_date, $form_state);
$errors[] = t('The To date must be greater than the From date.');
}
else {
// Convert input dates back to their UTC values and re-format to ISO
// or UNIX instead of the DATETIME format used in element processing.
$timezone = !empty($item[$tz_field]) ? $item[$tz_field] : $element['#date_timezone'];
$timezone_db = date_get_timezone_db($field['tz_handling']);
$item[$tz_field] = $timezone;
$from_date = date_make_date($from_date, $timezone);
$item[$offset_field] = date_offset_get($from_date);
$to_date = date_make_date($to_date, $timezone);
$test_from = date_format($from_date, 'r');
$test_to = date_format($to_date, 'r');
$item[$offset_field2] = date_offset_get($to_date);
date_timezone_set($from_date, timezone_open($timezone_db));
date_timezone_set($to_date, timezone_open($timezone_db));
$item[$from_field] = date_format($from_date, date_type_format($field['type']));
$item[$to_field] = date_format($to_date, date_type_format($field['type']));
if (isset($form_values[$field_name]['rrule'])) {
$item['rrule'] = $form_values[$field['field_name']]['rrule'];
}
// If the db timezone is not the same as the display timezone
// and we are using a date with time granularity,
// test a roundtrip back to the original timezone to catch
// invalid dates, like 2AM on the day that spring daylight savings
// time begins in the US.
$granularity = date_format_order($element[$from_field]['#date_format']);
if ($timezone != $timezone_db && date_has_time($granularity)) {
date_timezone_set($from_date, timezone_open($timezone));
date_timezone_set($to_date, timezone_open($timezone));
if ($test_from != date_format($from_date, 'r')) {
$errors[] = t('The From date is invalid.');
}
if ($test_to != date_format($to_date, 'r')) {
$errors[] = t('The To date is invalid.');
}
}
if (empty($errors)) {
form_set_value($element, $item, $form_state);
}
}
}
if (!empty($errors)) {
if ($field['multiple']) {
form_set_error($error_field, t('There are errors in @field_name value #@delta:', array(
'@field_name' => $field['widget']['label'],
'@delta' => $delta + 1,
)) . theme('item_list', $errors));
}
else {
form_set_error($error_field, t('There are errors in @field_name:', array(
'@field_name' => $field['widget']['label'],
)) . theme('item_list', $errors));
}
}
}