You are here

function bat_form_start_end_dates_validate in Booking and Availability Management Tools for Drupal 7

Validation callback that can be reused in all forms that need to validate dates.

The end date must be greater than start date.

1 string reference to 'bat_form_start_end_dates_validate'
bat_event_edit_form in modules/bat_event/bat_event.admin.inc
Form callback: create or edit a event.

File

./bat.module, line 659
Provides basic underlying functionality and configuration options used by all BAT modules.

Code

function bat_form_start_end_dates_validate($form, &$form_state) {
  list($start_date, $end_date) = bat_form_input_get_start_end_dates($form_state);

  // Fail validation if no dates are provided.
  if (empty($start_date) || empty($end_date)) {
    form_set_error('date_range', t('Please choose dates.'));
    return;
  }

  // Check date validity.
  $errors = bat_check_dates_validity($start_date, $end_date);

  // For some forms we need to ensure that the selected date matches with current values.
  if (isset($form_state['values']['curr_month']) && isset($form_state['values']['curr_year'])) {
    $curr_month = $form_state['values']['curr_month'];
    $curr_year = $form_state['values']['curr_year'];
    if ($start_date
      ->format('n') != $curr_month || $end_date
      ->format('n') != $curr_month || $start_date
      ->format('Y') != $curr_year || $end_date
      ->format('Y') != $curr_year) {
      $errors[] = t('Both the start and end date must be within the current month.');
    }
  }

  // When there are multiple errors for the same form element, Drupal only
  // displays the first. Here we concatenate all errors so that they will all
  // be displayed.
  if ($errors) {
    $error_msg = implode(' ', $errors);
    form_set_error('date_range', $error_msg);
  }
}