function webform_validation_validate in Webform Validation 7
Same name and namespace in other branches
- 6 webform_validation.module \webform_validation_validate()
Webform validation handler to validate against the given rules.
1 string reference to 'webform_validation_validate'
File
- ./
webform_validation.module, line 147
Code
function webform_validation_validate($form, &$form_state) {
$static_error_messages =& drupal_static(__FUNCTION__, array());
$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['webform']['page_count'])) {
$page_count = $form_state['webform']['page_count'];
}
elseif (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) {
$validators = webform_validation_get_validators();
foreach ($rules as $ruleid => $rule) {
// Skip the rule if it does not have any components on the current page.
if (!array_intersect_key($flat_values, $rule['components'])) {
unset($rules[$ruleid]);
}
elseif (isset($validators[$rule['validator']]['min_components']) && $validators[$rule['validator']]['min_components'] > 1) {
foreach (array_keys($rule['components']) as $cid) {
if ($node->webform['components'][$cid]['page_num'] > $form_state['webform']['page_num']) {
unset($rules[$ruleid]);
break;
}
}
}
}
}
if ($rules) {
// Remove hidden components.
if (defined('WebformConditionals::componentShown')) {
// New conditionals system.
$sorter = webform_get_conditional_sorter($node);
// If the form was retrieved from the form cache, the conditionals may not
// have been executed yet.
if (!$sorter
->isExecuted()) {
$sorter
->executeConditionals(array(), 0);
}
foreach ($node->webform['components'] as $key => $component) {
if ($sorter
->componentVisibility($component['cid'], $component['page_num']) !== WebformConditionals::componentShown) {
unset($flat_values[$key]);
}
}
}
else {
// Old conditionals system removed in Webform 7.x-4.8.
// Webform 7.x-3.x does not define WEBFORM_CONDITIONAL_INCLUDE.
// Define if needed.
if (!defined('WEBFORM_CONDITIONAL_INCLUDE')) {
define('WEBFORM_CONDITIONAL_INCLUDE', 1);
}
foreach ($node->webform['components'] as $key => $component) {
// In Webform 7.x-3.x, _webform_client_form_rule_check() returns
// boolean.
// Cast to int so that the function behaves as it does in 7.x-4.x.
if (isset($flat_values[$key]) && (int) _webform_client_form_rule_check($node, $component, 0, $form_state['values']['submitted']) !== WEBFORM_CONDITIONAL_INCLUDE) {
unset($flat_values[$key]);
}
}
}
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 (array_key_exists($cid, $flat_values)) {
$items[$cid] = $flat_values[$cid];
}
}
$rule['sid'] = $sid;
// Have the submitted values validated.
$components = webform_validation_prefix_keys($node->webform['components']);
// Allow translation for all components item names if available.
if (module_exists('webform_localization')) {
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
foreach ($components as &$component) {
$dummy_element = array(
'#title' => '',
);
_webform_localization_translate_component($dummy_element, $component);
if (isset($dummy_element['#title']) && (string) $dummy_element['#title']) {
$component['name'] = $dummy_element['#title'];
}
}
}
$errors = module_invoke_all("webform_validation_validate", $rule['validator'], webform_validation_prefix_keys($items), $components, $rule);
if ($errors) {
$errors = webform_validation_unprefix_keys($errors);
// Create hook_webform_validation_validate_alter(). Allow other modules
// to alter error messages.
$context = array(
'validator_name' => $rule['validator'],
'items' => $items,
'components' => $node->webform['components'],
'rule' => $rule,
);
drupal_alter('webform_validation_validate', $errors, $context);
foreach ($errors as $item_key => $error) {
// Do not set error message if an identical message has already been
// set.
if (in_array($error, $static_error_messages, TRUE)) {
continue;
}
$static_error_messages[] = $error;
// Build the proper form element error key, taking into account
// hierarchy.
$error_key = 'submitted][' . webform_validation_parent_tree($item_key, $node->webform['components']) . $node->webform['components'][$item_key]['form_key'];
if (is_array($error)) {
foreach ($error as $sub_item_key => $sub_error) {
form_set_error($error_key . '][' . $sub_item_key, $sub_error);
}
}
else {
// filter_xss() is run in _webform_validation_i18n_error_message().
// @ignore security_form_set_error.
form_set_error($error_key, $error);
}
}
}
}
}
}