You are here

function date_popup_validate in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_popup/date_popup.module \date_popup_validate()
  2. 6 date_popup/date_popup.module \date_popup_validate()
  3. 7.3 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_process in date_popup/date_popup.module
Javascript popup element processing. Add popup attributes to $element.

File

date_popup/date_popup.module, line 395
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;
  }
  $granularity = $element['#granularity'];
  $date_granularity = array_intersect($granularity, array(
    'month',
    'day',
    'year',
  ));
  $time_granularity = array_intersect($granularity, array(
    'hour',
    'minute',
    'second',
  ));
  $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  $label = t($label);

  // 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($element['#value']['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);
      }
    }
    form_set_value($element, NULL, $form_state);
    return;
  }
  require_once './' . drupal_get_path('module', 'date_api') . '/date_api_elements.inc';
  date_popup_load();
  $value = date_popup_input_value($element);

  // If the created date is valid, set it.
  if (!empty($value)) {
    form_set_value($element, $value, $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($date_granularity)) {
      form_set_error($error_field . '][date', $message);
      $message = ' ';
    }
    if (!empty($time_granularity)) {
      form_set_error($error_field . '][time', $message);
    }
  }
  form_set_value($element, NULL, $form_state);
}