You are here

function date_popup_validate in Date 7

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.3 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. Add popup attributes to $element.

File

date_popup/date_popup.module, line 347
A module to enable jquery calendar and time entry popups. Requires the Date API.

Code

function date_popup_validate($element, &$form_state) {
  if (is_string($element['#value'])) {
    return;
  }
  module_load_include('inc', 'date_api', 'date_api_elements');
  date_popup_add();
  $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);
  $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  $label = t($label);
  $input_exists = NULL;
  $input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists);
  $date = date_popup_input_date($element, $input);

  // If the field is empty and not required, set it to empty and return.
  // If the field is empty and required, set error message and return.
  $error_field = implode('][', $element['#parents']);
  if (empty($date)) {
    if ($element['#required']) {

      // Set message on both date and time to get them highlighted properly.
      $message = t('Field %field is required.', array(
        '%field' => $label,
      ));
      if (!empty($date_granularity)) {
        form_set_error($error_field . '][date', $message);
        $message = ' ';
      }
      if (!empty($time_granularity)) {
        form_set_error($error_field . '][time', $message);
        return;
      }
    }
    form_set_value($element, NULL, $form_state);
  }

  // If the created date is valid, set it.
  if (!empty($date)) {
    if (!empty($date->errors)) {
      form_error($element, implode('<br />', $date->errors));
      return;
    }
    form_set_value($element, $date
      ->format(DATE_FORMAT_DATETIME), $form_state);
    return;
  }
  else {

    // Set message on both date and time to get them highlighted properly.
    $message = t('Field %field is invalid.', array(
      '%field' => $label,
    ));
    if (!empty($element['#date_granularity'])) {
      form_set_error($error_field . '][date', $message);
      $message = ' ';
    }
    if (!empty($element['#time_granularity'])) {
      form_set_error($error_field . '][time', $message);
    }
  }
  form_set_value($element, NULL, $form_state);
}