function date_select_validate in Date 7.3
Same name and namespace in other branches
- 5.2 date_api_elements.inc \date_select_validate()
- 6.2 date_api_elements.inc \date_select_validate()
- 6 date_api_elements.inc \date_select_validate()
- 7 date_api/date_api_elements.inc \date_select_validate()
- 7.2 date_api/date_api_elements.inc \date_select_validate()
Validation function for date selector.
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_select_validate'
- date_select_element_process in date_api/
date_api_elements.inc - Flexible date/time drop-down selector.
File
- date_api/
date_api_elements.inc, line 711 - Date API elements themes and validation.
Code
function date_select_validate($element, &$form_state) {
if (date_hidden_element($element)) {
return;
}
if (is_string($element['#value'])) {
return;
}
$input_exists = NULL;
$input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
// Strip field labels out of the results.
foreach ($element['#value'] as $field => $field_value) {
if (substr($field_value, 0, 1) == '-') {
$input[$field] = '';
}
}
// Trigger hook_date_select_pre_validate_alter().
drupal_alter('date_select_pre_validate', $element, $form_state, $input);
$label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
if (isset($input['ampm'])) {
if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
$input['hour'] += 12;
}
elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
$input['hour'] -= 12;
}
}
unset($input['ampm']);
$date = date_select_input_date($element, $input);
// If the field has errors, display them.
$error_field = implode('][', $element['#parents']);
$entered = array_values(array_filter($input));
if (empty($date) || !empty($date->errors)) {
// The input created a date but it has errors.
if (is_object($date) && !empty($date->errors)) {
$message = t('The value input for field %field is invalid:', array(
'%field' => $label,
));
$message .= '<br />' . implode('<br />', $date->errors);
form_set_error($error_field, $message);
return;
}
elseif (empty($entered) && $element['#required']) {
$message = t('A valid date is required for %title.', array(
'%title' => $label,
));
form_set_error($error_field, $message);
return;
}
elseif (!empty($entered)) {
$message = t('The value input for field %field is invalid.', array(
'%field' => $label,
));
form_set_error($error_field, $message);
return;
}
}
$value = !empty($date) ? $date
->format(DATE_FORMAT_DATETIME) : NULL;
form_set_value($element, $value, $form_state);
}