function date_combo_validate in Date 7.3
Same name and namespace in other branches
- 8 date_elements.inc \date_combo_validate()
- 5.2 date/date_elements.inc \date_combo_validate()
- 6.2 date/date_elements.inc \date_combo_validate()
- 6 date/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_element_info in ./
date.module - Implements hook_element_info().
- date_field_widget_form in ./
date_elements.inc - Private implementation of hook_widget().
File
- ./
date_elements.inc, line 481 - Date forms and form themes and validation.
Code
function date_combo_validate($element, &$form_state) {
// Disabled and hidden elements won't have any input and don't need
// validation, we just need to re-save the original values, from before they
// were processed into widget arrays and timezone-adjusted.
if (date_hidden_element($element) || !empty($element['#disabled'])) {
form_set_value($element, $element['#date_items'], $form_state);
return;
}
$field_name = $element['#field_name'];
$delta = $element['#delta'];
$langcode = $element['#language'];
// Related issue: https://drupal.org/node/2279831.
if (!is_array($element['#field_parents'])) {
$element['#field_parents'] = array();
}
$form_values = drupal_array_get_nested_value($form_state['values'], $element['#field_parents']);
$form_input = drupal_array_get_nested_value($form_state['input'], $element['#field_parents']);
// Programmatically calling drupal_submit_form() does not always add the date
// combo to $form_state['input'].
if (empty($form_input[$field_name]) && !empty($form_values[$field_name])) {
form_set_value($element, $element['#date_items'], $form_state);
return;
}
// If the whole field is empty and that's OK, stop now.
if (empty($form_input[$field_name]) && !$element['#required']) {
return;
}
$item = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
$posted = drupal_array_get_nested_value($form_state['input'], $element['#parents']);
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$context = array(
'field' => $field,
'instance' => $instance,
'item' => $item,
);
// Trigger hook_date_combo_pre_validate_alter().
drupal_alter('date_combo_pre_validate', $element, $form_state, $context);
$from_field = 'value';
$to_field = 'value2';
$tz_field = 'timezone';
$offset_field = 'offset';
$offset_field2 = 'offset2';
// Check for empty 'Start 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;
}
}
}
}
// An 'End' date without a 'Start' date is a validation error.
if ($empty && !empty($item[$to_field])) {
if (!is_array($item[$to_field])) {
form_error($element, t("A 'Start date' date is required if an 'end date' is supplied for field %field #%delta.", array(
'%delta' => $field['cardinality'] ? intval($delta + 1) : '',
'%field' => $instance['label'],
)));
$empty = FALSE;
}
else {
foreach ($item[$to_field] as $key => $value) {
if (!empty($value)) {
form_error($element, t("A 'Start date' date is required if an 'End date' is supplied for field %field #%delta.", array(
'%delta' => $field['cardinality'] ? intval($delta + 1) : '',
'%field' => $instance['label'],
)));
$empty = FALSE;
break;
}
}
}
}
// If the user chose the option to not show the end date, just swap in the
// start date as that value so the start and end dates are the same.
if ($field['settings']['todate'] == 'optional' && empty($item['show_todate'])) {
$item[$to_field] = $item[$from_field];
$posted[$to_field] = $posted[$from_field];
}
if ($empty) {
$item = date_element_empty($element, $form_state);
if (!$element['#required']) {
return;
}
}
else {
$timezone = !empty($item[$tz_field]) ? $item[$tz_field] : $element['#date_timezone'];
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
$element[$from_field]['#date_timezone'] = $timezone;
$from_date = date_input_date($field, $instance, $element[$from_field], $posted[$from_field]);
if (!empty($field['settings']['todate'])) {
$element[$to_field]['#date_timezone'] = $timezone;
$to_date = date_input_date($field, $instance, $element[$to_field], $posted[$to_field]);
}
else {
$to_date = $from_date;
}
// Neither the start date nor the end date should be empty at this point
// unless they held values that couldn't be evaluated.
if (!$instance['required'] && (!date_is_date($from_date) || !date_is_date($to_date))) {
$item = date_element_empty($element, $form_state);
$errors[] = t('The dates are invalid.');
}
elseif (!empty($field['settings']['todate']) && $from_date > $to_date) {
form_set_value($element[$to_field], $to_date, $form_state);
$errors[] = t('The End date must be greater than the Start 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.
$item[$tz_field] = $timezone;
// Update the context for changes in the $item, and allow other modules to
// alter the computed local dates.
$context['item'] = $item;
// We can only pass two additional values to drupal_alter, so $element
// needs to be included in $context.
$context['element'] = $element;
// Trigger hook_date_combo_validate_date_start_alter().
drupal_alter('date_combo_validate_date_start', $from_date, $form_state, $context);
// Trigger hook_date_combo_validate_date_end_alter().
drupal_alter('date_combo_validate_date_end', $to_date, $form_state, $context);
$item[$offset_field] = date_offset_get($from_date);
$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 Start date is invalid.');
}
if ($test_to != date_format($to_date, 'r')) {
$errors[] = t('The End date is invalid.');
}
}
if (empty($errors)) {
form_set_value($element, $item, $form_state);
}
}
}
// Don't show further errors if errors are already flagged because otherwise
// we'll show errors on the nested elements more than once.
if (!form_get_errors() && !empty($errors)) {
if ($field['cardinality']) {
form_error($element, t('There are errors in @field_name value #@delta:', array(
'@field_name' => $instance['label'],
'@delta' => $delta + 1,
)) . theme('item_list', array(
'items' => $errors,
)));
}
else {
form_error($element, t('There are errors in @field_name:', array(
'@field_name' => $instance['label'],
)) . theme('item_list', array(
'items' => $errors,
)));
}
}
}