You are here

function date_popup_validate in Date 5.2

Same name and namespace in other branches
  1. 6.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.

File

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

Code

function date_popup_validate($element) {
  $granularity = $element['#granularity'];
  $date_granularity = array_intersect($granularity, array(
    'month',
    'day',
    'year',
  ));
  $time_granularity = array_intersect($granularity, array(
    'hour',
    'minute',
    'second',
  ));

  // 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 is required.', array(
        '%field' => $element['#title'],
      ));
      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);
    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 is invalid.', array(
      '%field' => $element['#title'],
    ));
    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);
}