function date_select_validate in Date 5.2
Same name and namespace in other branches
- 6.2 date_api_elements.inc \date_select_validate()
- 6 date_api_elements.inc \date_select_validate()
- 7.3 date_api/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.
File
- ./
date_api_elements.inc, line 402 - Date API elements themes and validation. This file is only included during the edit process to reduce memory usage.
Code
function date_select_validate($element) {
if (is_array($element['#value'])) {
// Strip field labels out of the results.
foreach ($element['#value'] as $field => $field_value) {
if (substr($field_value, 0, 1) == '-') {
$element['#value'][$field] = '';
}
}
}
$error_field = implode('][', $element['#parents']);
$errors = array();
if (in_array('year', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['year']))) {
if ($element['#value']['year'] < variable_get('date_min_year', 1) || $element['#value']['year'] > variable_get('date_max_year', 4000)) {
$errors[] = t('The year must be a number between %min and %max.', array(
'%min' => variable_get('date_min_year', 1),
'%max' => variable_get('date_max_year', 4000),
));
}
else {
$year = $element['#value']['year'];
}
}
if (in_array('month', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['month']))) {
if ($element['#value']['month'] < 1 || $element['#value']['month'] > 12) {
$errors[] = t('The month must be a number between 1 and 12.');
}
else {
$month = $element['#value']['month'];
}
}
if (in_array('day', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['day']))) {
$min = 1;
$max = isset($year) && isset($month) ? date_days_in_month($year, $month) : 31;
if ($element['#value']['day'] < $min || $element['#value']['day'] > $max) {
$errors[] = t('The day must be a number between !min and !max.', array(
'!min' => $min,
'!max' => $max,
));
}
}
if (in_array('hour', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['hour']))) {
$min = isset($element['#value']['ampm']) ? 1 : 0;
$max = isset($element['#value']['ampm']) ? 12 : 23;
if ($element['#value']['hour'] < $min || $element['#value']['hour'] > $max) {
$errors[] = t('The hour must be a number between !min and !max.', array(
'!min' => $min,
'!max' => $max,
));
}
}
if (in_array('minute', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['minute']))) {
$min = 0;
$max = 59;
if ($element['#value']['minute'] < $min || $element['#value']['minute'] > $max) {
$errors[] = t('The minute must be a number between !min and !max.', array(
'!min' => $min,
'!max' => $max,
));
}
}
if (in_array('second', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['second']))) {
$min = 0;
$max = 59;
if ($element['#value']['second'] < $min || $element['#value']['second'] > $max) {
$errors[] = t('The second must be a number between !min and !max.', array(
'!min' => $min,
'!max' => $max,
));
}
}
if (isset($element['#value']['ampm'])) {
if ($element['#value']['ampm'] == 'pm' && $element['#value']['hour'] < 12) {
$element['#value']['hour'] += 12;
}
elseif ($element['#value']['ampm'] == 'am' && $element['#value']['hour'] == 12) {
$element['#value']['hour'] -= 12;
}
}
$value = date_select_input_value($element);
if (empty($value) && empty($errors) && $element['#required']) {
$errors[] = t('A valid value is required.');
}
if (!empty($errors)) {
array_unshift($errors, t('%field has errors.', array(
'%field' => $element['#title'],
)));
form_set_error($error_field, implode(' ', $errors));
}
// If there are no errors and the value is valid, set it.
if (empty($errors) && !empty($value)) {
form_set_value($element, $value);
}
else {
form_set_value($element, NULL);
}
}