You are here

function date_text_validate in Date 7.2

Same name and namespace in other branches
  1. 5.2 date_api_elements.inc \date_text_validate()
  2. 5 date.inc \date_text_validate()
  3. 6.2 date_api_elements.inc \date_text_validate()
  4. 6 date_api_elements.inc \date_text_validate()
  5. 7.3 date_api/date_api_elements.inc \date_text_validate()
  6. 7 date_api/date_api_elements.inc \date_text_validate()

Validation for text input.

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_text_validate'
date_text_element_process in date_api/date_api_elements.inc
Text date input form.

File

date_api/date_api_elements.inc, line 379
Date API elements themes and validation.

Code

function date_text_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);

  // Trim extra spacing off user input of text fields.
  if (isset($input['date'])) {
    $input['date'] = trim($input['date']);
  }

  // Trigger hook_date_text_pre_validate_alter().
  drupal_alter('date_text_pre_validate', $element, $form_state, $input);
  $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  $date = date_text_input_date($element, $input);

  // If the field has errors, display them. If something was input but there is
  // no date, the date is invalid. If the field is empty and required, set
  // error message and return.
  $error_field = implode('][', $element['#parents']);
  if (empty($date) || !empty($date->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;
    }
    if (!empty($element['#required'])) {
      $message = t('A valid date is required for %title.', array(
        '%title' => $label,
      ));
      form_set_error($error_field, $message);
      return;
    }

    // Fall through, some other error.
    if (!empty($input['date'])) {
      form_error($element, t('%title is invalid.', array(
        '%title' => $label,
      )));
      return;
    }
  }
  $value = !empty($date) ? $date
    ->format(DATE_FORMAT_DATETIME) : NULL;
  form_set_value($element, $value, $form_state);
}