You are here

function _webform_client_form_validate in Webform 6.3

Same name and namespace in other branches
  1. 7.4 webform.module \_webform_client_form_validate()
  2. 7.3 webform.module \_webform_client_form_validate()

Recursive validation function to trigger normal Drupal validation.

This function imitates _form_validate in Drupal's form.inc, only it sets a different property to ensure that validation has occurred.

1 call to _webform_client_form_validate()
webform_client_form_validate in ./webform.module

File

./webform.module, line 2142

Code

function _webform_client_form_validate($elements, &$form_state, $first_run = TRUE) {
  static $form;
  if ($first_run) {
    $form = $elements;
  }

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

  // Validate the current input.
  if (isset($elements['#webform_validated']) && $elements['#webform_validated'] == FALSE) {
    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. For
      // radios, FALSE means that no value was submitted, so check that too.
      if ($elements['#required'] && (!count($elements['#value']) || is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0 || $elements['#value'] === FALSE)) {
        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']),
        )));
      }
      if (isset($elements['#options']) && isset($elements['#value'])) {
        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', 'Illegal choice %choice in !name element.', array(
                '%choice' => $v,
                '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
              ), WATCHDOG_ERROR);
            }
          }
        }
        elseif ($elements['#value'] !== '' && !isset($options[$elements['#value']])) {
          form_error($elements, t('An illegal choice has been detected. Please contact the site administrator.'));
          watchdog('form', 'Illegal choice %choice in %name element.', array(
            '%choice' => $elements['#value'],
            '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
          ), WATCHDOG_ERROR);
        }
      }
    }

    // Call any element-specific validators. These must act on the element
    // #value data.
    if (isset($elements['#element_validate'])) {
      foreach ($elements['#element_validate'] as $function) {
        if (function_exists($function)) {
          $function($elements, $form_state, $form);
        }
      }
    }
    $elements['#webform_validated'] = TRUE;
  }
}