public function availability_calendar_handler_filter_availability::validate_value in Availability Calendars 7.5
Same name and namespace in other branches
- 7.3 availability_calendar_handler_filter_availability.inc \availability_calendar_handler_filter_availability::validate_value()
- 7.4 views/availability_calendar_handler_filter_availability.inc \availability_calendar_handler_filter_availability::validate_value()
Validate that the values convert to something usable.
Parameters
array $element:
array $form_state:
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 401
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;
$autoSubmit = FALSE;
$operator = $form_state['values']['options']['operator'];
}
else {
// In exposed forms, values are required if "Required" was checked and if
// this is not an auto-submitted form.
$required = (bool) $this->options['expose']['required'];
$autoSubmit = (bool) $form_state['exposed_form_plugin']->options['autosubmit'];
$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;
$requireFrom = FALSE;
if (in_array('from', $values) && array_key_exists('from', $value)) {
$from_valid = $this
->validate_valid_time_1($element['from'], $value['from'], $required && !$autoSubmit, $today, t('Only future availability can be searched.'));
}
if (in_array('to', $values) && array_key_exists('to', $value)) {
$to_valid = $this
->validate_valid_time_1($element['to'], $value['to'], ($required || $from_valid instanceof DateTime) && !$autoSubmit, $from_valid, t('The end date should be on or after the start date.'));
if ($to_valid instanceof DateTime) {
$requireFrom = TRUE;
}
}
if (in_array('to1', $values) && array_key_exists('to1', $value)) {
$to1_valid = $this
->validate_valid_time_1($element['to1'], $value['to1'], ($required || $from_valid instanceof DateTime) && !$autoSubmit, $from_valid, t('The departure date should be after the arrival date.'));
if ($to1_valid instanceof DateTime) {
$requireFrom = TRUE;
}
}
if (in_array('duration', $values) && array_key_exists('duration', $value)) {
$duration_valid = $this
->validate_valid_duration($element['duration'], $value['duration'], ($required || $from_valid instanceof DateTime) && !$autoSubmit);
if ($duration_valid && !empty($value['duration'])) {
$requireFrom = TRUE;
}
}
// Do a 2nd check on from based on whether a 2nd operand was filled in,
// making from required for non auto submitting forms.
if ($from_valid && $requireFrom && !$autoSubmit && empty($value['from'])) {
form_error($element['from'], t('Field %field is required.', array(
'%field' => $element['from']['#title'],
)));
}
}