You are here

function _form_validate in Drupal 5

Same name and namespace in other branches
  1. 4 includes/form.inc \_form_validate()
  2. 6 includes/form.inc \_form_validate()
  3. 7 includes/form.inc \_form_validate()

Performs validation on form elements. First ensures required fields are completed, #maxlength is not exceeded, and selected options were in the list of options given to the user. Then calls user-defined validators.

Parameters

$elements: An associative array containing the structure of the form.

$form_id: A unique string identifying the form for validation, submission, theming, and hook_form_alter functions.

Related topics

1 call to _form_validate()
drupal_validate_form in includes/form.inc
Validates user-submitted form data from a global variable using the validate functions defined in a structured form array.

File

includes/form.inc, line 522

Code

function _form_validate($elements, $form_id = NULL) {

  // Recurse through all children.
  foreach (element_children($elements) as $key) {
    if (isset($elements[$key]) && $elements[$key]) {
      _form_validate($elements[$key]);
    }
  }

  /* Validate the current input */
  if (!isset($elements['#validated']) || !$elements['#validated']) {
    if (isset($elements['#needs_validation'])) {

      // Make sure a value is passed when the field is required.
      // A simple call to empty() will not cut it here as some fields, like
      // checkboxes, can return a valid value of '0'. Instead, check the
      // length if it's a string, and the item count if it's an array.
      if ($elements['#required'] && (!count($elements['#value']) || is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0)) {
        form_error($elements, t('!name field is required.', array(
          '!name' => $elements['#title'],
        )));
      }

      // Verify that the value is not longer than #maxlength.
      if (isset($elements['#maxlength']) && drupal_strlen($elements['#value']) > $elements['#maxlength']) {
        form_error($elements, t('!name cannot be longer than %max characters but is currently %length characters long.', array(
          '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
          '%max' => $elements['#maxlength'],
          '%length' => drupal_strlen($elements['#value']),
        )));
      }

      // Add legal choice check if element has #options. Can be skipped, but
      // then you must validate your own element.
      if (isset($elements['#options']) && isset($elements['#value']) && !isset($elements['#DANGEROUS_SKIP_CHECK'])) {
        if ($elements['#type'] == 'select') {
          $options = form_options_flatten($elements['#options']);
        }
        else {
          $options = $elements['#options'];
        }
        if (is_array($elements['#value'])) {
          $value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value'];
          foreach ($value as $v) {
            if (!isset($options[$v])) {
              form_error($elements, t('An illegal choice has been detected. Please contact the site administrator.'));
              watchdog('form', t('Illegal choice %choice in !name element.', array(
                '%choice' => $v,
                '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
              )), WATCHDOG_ERROR);
            }
          }
        }
        elseif (!isset($options[$elements['#value']])) {
          form_error($elements, t('An illegal choice has been detected. Please contact the site administrator.'));
          watchdog('form', t('Illegal choice %choice in %name element.', array(
            '%choice' => $elements['#value'],
            '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
          )), WATCHDOG_ERROR);
        }
      }
    }

    // Call user-defined validators.
    if (isset($elements['#validate'])) {
      foreach ($elements['#validate'] as $function => $args) {
        $args = array_merge(array(
          $elements,
        ), $args);

        // For the full form we hand over a copy of $form_values.
        if (isset($form_id)) {
          $args = array_merge(array(
            $form_id,
            $GLOBALS['form_values'],
          ), $args);
        }
        if (function_exists($function)) {
          call_user_func_array($function, $args);
        }
      }
    }
    $elements['#validated'] = TRUE;
  }
}