function date_combo_validate in Date 8
Same name and namespace in other branches
- 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.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.
4 string references to 'date_combo_validate'
- DateFieldWidgetBase::formElement in date_field/
lib/ Drupal/ date_field/ Plugin/ field/ widget/ DateFieldWidgetBase.php - Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
- DateTextWidget::formElement in lib/
Drupal/ date/ Plugin/ field/ widget/ DateTextWidget.php - Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
- DateWidgetBase::formElement in lib/
Drupal/ date/ Plugin/ field/ widget/ DateWidgetBase.php - Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
- date_element_info in ./
date.module - Implements hook_element_info().
File
- ./
date_elements.inc, line 251 - 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'];
$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']);
// If the whole field is empty and that's OK, stop now.
if (empty($form_input[$field_name]) && !$element['#required']) {
return;
}
// @TODO In latest field code there is no $delta in these arrays.
// This is only in Date, so it's a problem in this code.
$item = $form_values[$field_name][$langcode][$delta];
$posted = $form_input[$field_name][$langcode][$delta];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$format = date_get_format($instance);
$context = array(
'field' => $field,
'instance' => $instance,
'item' => $item,
);
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, $item, $form_state);
if (!$element['#required']) {
return;
}
}
elseif (!form_get_errors()) {
$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'] && ($from_date
->hasErrors() || $to_date
->hasErrors())) {
$item = date_element_empty($element, $item, $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;
drupal_alter('date_combo_validate_date_start', $from_date, $form_state, $context);
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($format);
if ($timezone != $timezone_db && DateGranularity::hasTime($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);
}
}
}
if (!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,
)));
}
}
}