You are here

function webform_validation_validate in Webform Validation 6

Same name and namespace in other branches
  1. 7 webform_validation.module \webform_validation_validate()

Webform validation handler to validate against the given rules

1 string reference to 'webform_validation_validate'
webform_validation_form_alter in ./webform_validation.module
Implementation of hook_form_alter().

File

./webform_validation.module, line 102

Code

function webform_validation_validate($form, &$form_state) {
  $page_count = 1;
  $nid = $form_state['values']['details']['nid'];
  $node = node_load($nid);
  $values = isset($form_state['values']['submitted']) ? $form_state['values']['submitted'] : NULL;
  $flat_values = _webform_client_form_submit_flatten($node, $values);
  $rules = webform_validation_get_node_rules($nid);
  $sid = empty($form_state['values']['details']['sid']) ? 0 : $form_state['values']['details']['sid'];

  // Get number of pages for this webform
  if (isset($form_state['values']['details']['page_count'])) {
    $page_count = $form_state['values']['details']['page_count'];
  }
  else {
    if (isset($form_state['webform']['page_count'])) {
      $page_count = $form_state['webform']['page_count'];
    }
    else {
      if (isset($form_state['storage']['page_count'])) {
        $page_count = $form_state['storage']['page_count'];
      }
    }
  }

  // Filter out rules that don't apply to this step in the multistep form
  if ($values && $page_count && $page_count > 1) {
    $current_page_components = webform_validation_get_field_keys($form_state['values']['submitted'], $node);
    if ($rules) {

      // filter out rules that don't belong in the current step
      foreach ($rules as $ruleid => $rule) {

        // get all the component formkeys for this specific validation rule
        $rule_formkeys = webform_validation_rule_get_formkeys($rule);
        $rule_applies_to_current_page = FALSE;
        if (!empty($rule_formkeys)) {
          foreach ($rule_formkeys as $formkey) {
            if (in_array($formkey, $current_page_components)) {

              // this rule applies to the current page,
              // because one of the rule components is on the page
              $rule_applies_to_current_page = TRUE;
            }
          }
        }
        if (!$rule_applies_to_current_page) {
          unset($rules[$ruleid]);
        }
      }
    }
  }
  if ($rules) {
    foreach ($rules as $rule) {

      // create a list of components that need validation against this rule (component id => user submitted value)
      $items = array();
      foreach ($rule['components'] as $cid => $component) {
        if (isset($flat_values[$cid])) {
          $items[$cid] = $flat_values[$cid];
        }
      }

      // prefix array keys to avoid reindexing by the module_invoke_all function call
      $items = webform_validation_prefix_keys($items);
      $component_definitions = webform_validation_prefix_keys($node->webform['components']);
      $rule['sid'] = $sid;

      // have the submitted values validated
      $errors = module_invoke_all("webform_validation_validate", $rule['validator'], $items, $component_definitions, $rule);
      if ($errors) {
        $errors = webform_validation_unprefix_keys($errors);
        $components = webform_validation_unprefix_keys($component_definitions);
        foreach ($errors as $item_key => $error) {

          // build the proper form element error key, taking into account hierarchy
          $error_key = 'submitted][' . webform_validation_parent_tree($item_key, $components) . $components[$item_key]['form_key'];
          form_set_error($error_key, $error);
        }
      }
    }
  }
}