You are here

public function availability_calendar_handler_filter_availability::validate_value in Availability Calendars 7.4

Same name and namespace in other branches
  1. 7.5 views/availability_calendar_handler_filter_availability.inc \availability_calendar_handler_filter_availability::validate_value()
  2. 7.3 availability_calendar_handler_filter_availability.inc \availability_calendar_handler_filter_availability::validate_value()

Validate that the values convert to something usable.

1 call to availability_calendar_handler_filter_availability::validate_value()
availability_calendar_handler_filter_availability::exposed_validate in views/availability_calendar_handler_filter_availability.inc
Validates our part of the exposed form.

File

views/availability_calendar_handler_filter_availability.inc, line 255

Class

availability_calendar_handler_filter_availability
Views handler to filter on availability.

Code

public function validate_value(&$element, $form_state) {
  if (empty($form_state['exposed'])) {

    // In Views UI, the value is required if the filter is not exposed,
    // otherwise we don't validate at all (so people can place "help texts" in
    // the inputs.)
    if ($form_state['values']['options']['expose_button']['checkbox']['checkbox']) {
      return;
    }
    $required = FALSE;
    $operator = $form_state['values']['options']['operator'];
  }
  else {

    // In exposed form, values are required if "Required" was checked.
    $required = (bool) $this->options['expose']['required'];
    $operator = empty($this->options['expose']['use_operator']) ? $this->operator : $form_state['values'][$this->options['expose']['operator_id']];
  }
  $operators = $this
    ->operators();
  $values = empty($operator) ? array(
    'from',
    'to',
    'to1',
    'duration',
  ) : $operators[$operator]['values'];

  // Set time to midnight as other dates are also set to that time.
  $today = new DateTime();
  $today
    ->setTime(0, 0, 0);
  $value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  $from_valid = FALSE;
  if (in_array('from', $values) && array_key_exists('from', $value)) {
    $from_valid = $this
      ->validate_valid_time_1($element['from'], $value['from'], $required, $today, t('Only future availability can be searched.'));
  }
  if (in_array('to', $values) && array_key_exists('to', $value)) {
    $this
      ->validate_valid_time_1($element['to'], $value['to'], $required || $from_valid instanceof DateTime, $from_valid, t('The end date should be on or after the start date.'));
  }
  if (in_array('to1', $values) && array_key_exists('to1', $value)) {
    $this
      ->validate_valid_time_1($element['to1'], $value['to1'], $required || $from_valid instanceof DateTime, $from_valid, t('The departure date should be after the arrival date.'));
  }
  if (in_array('duration', $values) && array_key_exists('duration', $value)) {
    $this
      ->validate_valid_duration($element['duration'], $value['duration'], $required || $from_valid instanceof DateTime);
  }
}