You are here

function date_popup_validate in Date 7.3

Same name and namespace in other branches
  1. 5.2 date_popup/date_popup.module \date_popup_validate()
  2. 6.2 date_popup/date_popup.module \date_popup_validate()
  3. 6 date_popup/date_popup.module \date_popup_validate()
  4. 7 date_popup/date_popup.module \date_popup_validate()
  5. 7.2 date_popup/date_popup.module \date_popup_validate()

Massage the input values back into a single date.

When used as a Views widget, the validation step always gets triggered, even with no form submission. Before form submission $element['#value'] contains a string, after submission it contains an array.

1 string reference to 'date_popup_validate'
date_popup_element_process in date_popup/date_popup.module
Javascript popup element processing.

File

date_popup/date_popup.module, line 575
A module to enable jQuery calendar and time entry popups.

Code

function date_popup_validate($element, &$form_state) {
  if (date_hidden_element($element)) {
    return;
  }
  if (is_string($element['#value'])) {
    return;
  }
  module_load_include('inc', 'date_api', 'date_api_elements');
  date_popup_add();
  $input_exists = NULL;
  $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);

  // If the date is a string, it is not considered valid and can cause problems
  // later on, so just exit out now.
  if (is_string($input)) {
    return;
  }

  // Trigger hook_date_popup_pre_validate_alter().
  drupal_alter('date_popup_pre_validate', $element, $form_state, $input);
  $granularity = date_format_order($element['#date_format']);
  $date_granularity = date_popup_date_granularity($element);
  $time_granularity = date_popup_time_granularity($element);
  $has_time = date_has_time($granularity);

  // @codingStandardsIgnoreStart
  $label = '';
  if (!empty($element['#date_title'])) {
    $label = t($element['#date_title']);
  }
  elseif (!empty($element['#title'])) {
    $label = t($element['#title']);
  }

  // @codingStandardsIgnoreEnd
  $date = date_popup_input_date($element, $input);

  // If the date has errors, display them. If something was input but there is
  // no date, the date is invalid. If the field is empty and required, set
  // error message and return.
  $error_field = implode('][', $element['#parents']);
  if (empty($element['#value']['date']) && empty($element['#value']['time']) || !empty($date->errors)) {
    if (is_object($date) && !empty($date->errors)) {
      $message = t('The value input for field %field is invalid:', array(
        '%field' => $label,
      ));
      $message .= '<br />' . implode('<br />', $date->errors);
      form_set_error($error_field, $message);
      return;
    }
    if (!empty($input['date'])) {
      $message = t('The value input for field %field is invalid.', array(
        '%field' => $label,
      ));
      form_set_error($error_field, $message);
      return;
    }
    if ($element['#required']) {
      $message = t('A valid date is required for %title.', array(
        '%title' => $label,
      ));
      form_set_error($error_field, $message);
      return;
    }
  }

  // If the created date is valid, set it.
  $value = !empty($date) ? $date
    ->format(DATE_FORMAT_DATETIME) : NULL;
  form_set_value($element, $value, $form_state);
}