Révision 81b16cc2
Ajouté par Assos Assos il y a plus de 6 ans
drupal7/sites/all/modules/webform_validation/webform_validation.module | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Add validation rules to webforms |
|
5 |
* Add validation rules to webforms.
|
|
6 | 6 |
*/ |
7 | 7 |
|
8 |
include_once('webform_validation.validators.inc');
|
|
9 |
include_once('webform_validation.rules.inc');
|
|
8 |
include_once 'webform_validation.validators.inc';
|
|
9 |
include_once 'webform_validation.rules.inc';
|
|
10 | 10 |
|
11 | 11 |
/** |
12 | 12 |
* Implements hook_menu(). |
... | ... | |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
/** |
62 |
* Loads validation rule from menu parameter |
|
62 |
* Loads validation rule from menu parameter.
|
|
63 | 63 |
*/ |
64 | 64 |
function webform_validation_rule_load($ruleid) { |
65 | 65 |
return webform_validation_get_rule($ruleid); |
66 | 66 |
} |
67 | 67 |
|
68 |
|
|
69 | 68 |
/** |
70 | 69 |
* Implements hook_theme(). |
71 | 70 |
*/ |
... | ... | |
99 | 98 |
$groups['webform_validation'] = array( |
100 | 99 |
'title' => t('Webform Validation'), |
101 | 100 |
'description' => t('Translatable strings for webform validation translation'), |
102 |
'format' => FALSE, // This group doesn't have strings with format |
|
103 |
'list' => FALSE, // This group cannot list all strings |
|
101 |
// This group doesn't have strings with format. |
|
102 |
'format' => FALSE, |
|
103 |
// This group cannot list all strings. |
|
104 |
'list' => FALSE, |
|
104 | 105 |
'refresh callback' => 'webform_validation_i18n_string_refresh', |
105 | 106 |
); |
106 | 107 |
return $groups; |
107 | 108 |
} |
108 | 109 |
|
109 | 110 |
/** |
110 |
* Webform validation handler to validate against the given rules |
|
111 |
* Webform validation handler to validate against the given rules.
|
|
111 | 112 |
*/ |
112 | 113 |
function webform_validation_validate($form, &$form_state) { |
113 | 114 |
$static_error_messages = &drupal_static(__FUNCTION__, array()); |
... | ... | |
119 | 120 |
$rules = webform_validation_get_node_rules($nid); |
120 | 121 |
$sid = empty($form_state['values']['details']['sid']) ? 0 : $form_state['values']['details']['sid']; |
121 | 122 |
|
122 |
// Get number of pages for this webform |
|
123 |
// Get number of pages for this webform.
|
|
123 | 124 |
if (isset($form_state['webform']['page_count'])) { |
124 | 125 |
$page_count = $form_state['webform']['page_count']; |
125 | 126 |
} |
... | ... | |
127 | 128 |
$page_count = $form_state['storage']['page_count']; |
128 | 129 |
} |
129 | 130 |
|
130 |
// Filter out rules that don't apply to this step in the multistep form |
|
131 |
// Filter out rules that don't apply to this step in the multistep form.
|
|
131 | 132 |
if ($values && $page_count && $page_count > 1) { |
132 |
$current_page_components = webform_validation_get_field_keys($form_state['values']['submitted'], $node); |
|
133 |
if ($rules) { |
|
134 |
// filter out rules that don't belong in the current step |
|
135 |
foreach ($rules as $ruleid => $rule) { |
|
136 |
// get all the component formkeys for this specific validation rule |
|
137 |
$rule_formkeys = webform_validation_rule_get_formkeys($rule); |
|
138 |
$rule_applies_to_current_page = FALSE; |
|
139 |
if (!empty($rule_formkeys)) { |
|
140 |
foreach ($rule_formkeys as $formkey) { |
|
141 |
if (in_array($formkey, $current_page_components)) { |
|
142 |
// this rule applies to the current page, |
|
143 |
// because one of the rule components is on the page |
|
144 |
$rule_applies_to_current_page = TRUE; |
|
145 |
} |
|
133 |
$validators = webform_validation_get_validators(); |
|
134 |
foreach ($rules as $ruleid => $rule) { |
|
135 |
// Skip the rule if it does not have any components on the current page. |
|
136 |
if (!array_intersect_key($flat_values, $rule['components'])) { |
|
137 |
unset($rules[$ruleid]); |
|
138 |
} |
|
139 |
// For validators that require at least 2 components, skip the rule if any |
|
140 |
// of the components are on a page past the current page. |
|
141 |
elseif (isset($validators[$rule['validator']]['min_components']) && $validators[$rule['validator']]['min_components'] > 1) { |
|
142 |
foreach (array_keys($rule['components']) as $cid) { |
|
143 |
if ($node->webform['components'][$cid]['page_num'] > $form_state['webform']['page_num']) { |
|
144 |
unset($rules[$ruleid]); |
|
145 |
break; |
|
146 | 146 |
} |
147 | 147 |
} |
148 |
|
|
149 |
if (!$rule_applies_to_current_page) { |
|
150 |
unset($rules[$ruleid]); |
|
151 |
} |
|
152 | 148 |
} |
153 | 149 |
} |
154 | 150 |
} |
... | ... | |
173 | 169 |
} |
174 | 170 |
else { |
175 | 171 |
// Old conditionals system removed in Webform 7.x-4.8. |
176 |
// Webform 7.x-3.x does not define WEBFORM_CONDITIONAL_INCLUDE. Define if needed. |
|
172 |
// Webform 7.x-3.x does not define WEBFORM_CONDITIONAL_INCLUDE. |
|
173 |
// Define if needed. |
|
177 | 174 |
if (!defined('WEBFORM_CONDITIONAL_INCLUDE')) { |
178 | 175 |
define('WEBFORM_CONDITIONAL_INCLUDE', 1); |
179 | 176 |
} |
180 | 177 |
foreach ($component_definitions as $key => $component) { |
181 |
// In Webform 7.x-3.x, _webform_client_form_rule_check() returns boolean. |
|
178 |
// In Webform 7.x-3.x, _webform_client_form_rule_check() returns |
|
179 |
// boolean. |
|
182 | 180 |
// Cast to int so that the function behaves as it does in 7.x-4.x. |
183 | 181 |
if (isset($flat_values[$key]) && (int) _webform_client_form_rule_check($node, $component, 0, $form_state['values']['submitted']) !== WEBFORM_CONDITIONAL_INCLUDE) { |
184 | 182 |
unset($flat_values[$key]); |
... | ... | |
187 | 185 |
} |
188 | 186 |
|
189 | 187 |
foreach ($rules as $rule) { |
190 |
// create a list of components that need validation against this rule (component id => user submitted value) |
|
188 |
// Create a list of components that need validation against this rule |
|
189 |
// (component id => user submitted value). |
|
191 | 190 |
$items = array(); |
192 | 191 |
foreach ($rule['components'] as $cid => $component) { |
193 | 192 |
if (array_key_exists($cid, $flat_values)) { |
194 | 193 |
$items[$cid] = $flat_values[$cid]; |
195 | 194 |
} |
196 | 195 |
} |
197 |
// prefix array keys to avoid reindexing by the module_invoke_all function call |
|
196 |
// Prefix array keys to avoid reindexing by the module_invoke_all function |
|
197 |
// call. |
|
198 | 198 |
$items = webform_validation_prefix_keys($items); |
199 | 199 |
$rule['sid'] = $sid; |
200 |
// have the submitted values validated
|
|
200 |
// Have the submitted values validated.
|
|
201 | 201 |
$errors = module_invoke_all("webform_validation_validate", $rule['validator'], $items, $component_definitions, $rule); |
202 | 202 |
if ($errors) { |
203 | 203 |
$errors = webform_validation_unprefix_keys($errors); |
204 | 204 |
$components = webform_validation_unprefix_keys($component_definitions); |
205 | 205 |
foreach ($errors as $item_key => $error) { |
206 |
// Do not set error message if an identical message has already been set. |
|
206 |
// Do not set error message if an identical message has already been |
|
207 |
// set. |
|
207 | 208 |
if (in_array($error, $static_error_messages, TRUE)) { |
208 | 209 |
continue; |
209 | 210 |
} |
210 | 211 |
$static_error_messages[] = $error; |
211 | 212 |
|
212 |
// build the proper form element error key, taking into account hierarchy |
|
213 |
// Build the proper form element error key, taking into account |
|
214 |
// hierarchy. |
|
213 | 215 |
$error_key = 'submitted][' . webform_validation_parent_tree($item_key, $components) . $components[$item_key]['form_key']; |
214 | 216 |
if (is_array($error)) { |
215 | 217 |
foreach ($error as $sub_item_key => $sub_error) { |
... | ... | |
217 | 219 |
} |
218 | 220 |
} |
219 | 221 |
else { |
220 |
// @ignore security_form_set_error. filter_xss() is run in _webform_validation_i18n_error_message(). |
|
222 |
// filter_xss() is run in _webform_validation_i18n_error_message(). |
|
223 |
// @ignore security_form_set_error. |
|
221 | 224 |
form_set_error($error_key, $error); |
222 | 225 |
} |
223 | 226 |
} |
... | ... | |
227 | 230 |
} |
228 | 231 |
|
229 | 232 |
/** |
230 |
* Recursive helper function to get all field keys (including fields in fieldsets) |
|
233 |
* Helper function to get all field keys (including fields in fieldsets). |
|
234 |
* |
|
235 |
* @deprecated No longer used and will be removed in 7.x-2.x. |
|
231 | 236 |
*/ |
232 | 237 |
function webform_validation_get_field_keys($submitted, $node) { |
233 | 238 |
static $fields = array(); |
234 | 239 |
foreach (element_children($submitted) as $child) { |
235 | 240 |
if (is_array($submitted[$child]) && element_children($submitted[$child])) { |
236 |
// only keep searching recursively if it's a fieldset
|
|
241 |
// Only keep searching recursively if it's a fieldset.
|
|
237 | 242 |
$group_components = _webform_validation_get_group_types(); |
238 | 243 |
if (in_array(_webform_validation_get_component_type($node, $child), $group_components)) { |
239 | 244 |
webform_validation_get_field_keys($submitted[$child], $node); |
... | ... | |
251 | 256 |
} |
252 | 257 |
|
253 | 258 |
/** |
254 |
* Recursively add the parents for the element, to be used as first argument to form_set_error |
|
259 |
* Recursively add the parents for the element, to be used as first argument to form_set_error.
|
|
255 | 260 |
*/ |
256 | 261 |
function webform_validation_parent_tree($cid, $components) { |
257 | 262 |
$output = ''; |
... | ... | |
263 | 268 |
} |
264 | 269 |
|
265 | 270 |
/** |
266 |
* Get an array of formkeys for all components that have been assigned to a rule |
|
271 |
* Get array of formkeys for all components that have been assigned to a rule. |
|
272 |
* |
|
273 |
* @deprecated No longer used and will be removed in 7.x-2.x. |
|
267 | 274 |
*/ |
268 | 275 |
function webform_validation_rule_get_formkeys($rule) { |
269 | 276 |
$formkeys = array(); |
... | ... | |
276 | 283 |
} |
277 | 284 |
|
278 | 285 |
/** |
279 |
* Prefix numeric array keys to avoid them being reindexed by module_invoke_all |
|
286 |
* Prefix numeric array keys to avoid them being reindexed by module_invoke_all.
|
|
280 | 287 |
*/ |
281 | 288 |
function webform_validation_prefix_keys($arr) { |
282 | 289 |
$ret = array(); |
... | ... | |
287 | 294 |
} |
288 | 295 |
|
289 | 296 |
/** |
290 |
* Undo prefixing numeric array keys to avoid them being reindexed by module_invoke_all |
|
297 |
* Undo prefixing numeric array keys to avoid them being reindexed by module_invoke_all.
|
|
291 | 298 |
*/ |
292 | 299 |
function webform_validation_unprefix_keys($arr) { |
293 | 300 |
$ret = array(); |
... | ... | |
297 | 304 |
} |
298 | 305 |
return $ret; |
299 | 306 |
} |
307 |
|
|
300 | 308 |
/** |
301 |
* Theme the 'add rule' list |
|
309 |
* Theme the 'add rule' list.
|
|
302 | 310 |
*/ |
303 | 311 |
function theme_webform_validation_manage_add_rule($variables) { |
304 | 312 |
$nid = $variables['nid']; |
... | ... | |
368 | 376 |
} |
369 | 377 |
|
370 | 378 |
/** |
371 |
* Adds support for node_clone module |
|
379 |
* Adds support for node_clone module.
|
|
372 | 380 |
*/ |
373 | 381 |
function webform_validation_node_clone($node) { |
374 | 382 |
if (!in_array($node->type, webform_variable_get('webform_node_types'))) { |
... | ... | |
376 | 384 |
} |
377 | 385 |
if (isset($node->clone_from_original_nid)) { |
378 | 386 |
$original_nid = $node->clone_from_original_nid; |
379 |
// Get existing rules for original node |
|
387 |
// Get existing rules for original node.
|
|
380 | 388 |
$rules = webform_validation_get_node_rules($original_nid); |
381 | 389 |
if ($rules) { |
382 | 390 |
foreach ($rules as $orig_ruleid => $rule) { |
383 | 391 |
unset($rule['ruleid']); |
384 | 392 |
$rule['action'] = 'add'; |
385 |
$rule['nid'] = $node->nid; // attach existing rules to new node |
|
393 |
// Attach existing rules to new node. |
|
394 |
$rule['nid'] = $node->nid; |
|
386 | 395 |
$rule['rule_components'] = $rule['components']; |
387 | 396 |
webform_validation_rule_save($rule); |
388 | 397 |
} |
... | ... | |
391 | 400 |
} |
392 | 401 |
|
393 | 402 |
/** |
394 |
* Save a validation rule. Data comes from the admin form or nodeapi function in |
|
395 |
* case of node clone. |
|
403 |
* Save a validation rule. |
|
404 |
* |
|
405 |
* Data comes from the admin form or nodeapi function in case of node clone. |
|
396 | 406 |
* |
397 | 407 |
* @param array $values |
398 | 408 |
* An associative array containing: |
... | ... | |
407 | 417 |
* @return int |
408 | 418 |
* The $ruleid of the rule added or edited. |
409 | 419 |
*/ |
410 |
function webform_validation_rule_save($values) { |
|
420 |
function webform_validation_rule_save(array $values) {
|
|
411 | 421 |
if ($values['action'] === 'add') { |
412 | 422 |
$primary_keys = array(); |
413 | 423 |
} |
... | ... | |
442 | 452 |
* @param int $ruleid |
443 | 453 |
* The ruleid of the rule being saved. |
444 | 454 |
* @param array $components |
445 |
* An array in which the keys are the cid's of the components attached to the rule. |
|
455 |
* An array in which the keys are the cid's of the components attached to the |
|
456 |
* rule. |
|
457 |
* |
|
446 | 458 |
* @return array |
447 | 459 |
* An array of the return statuses for each query keyed by cid. |
448 | 460 |
*/ |
449 |
function webform_validation_save_rule_components($ruleid, $components) { |
|
461 |
function webform_validation_save_rule_components($ruleid, array $components) {
|
|
450 | 462 |
$return_status = array(); |
451 | 463 |
foreach ($components as $cid => $component) { |
452 | 464 |
$return_status[$cid] = db_merge('webform_validation_rule_components') |
... | ... | |
464 | 476 |
} |
465 | 477 |
|
466 | 478 |
/** |
467 |
* Given a webform node, get the component type based on a given component key |
|
479 |
* Given a webform node, get the component type based on a given component key.
|
|
468 | 480 |
*/ |
469 | 481 |
function _webform_validation_get_component_type($node, $component_key) { |
470 | 482 |
if ($node->webform['components']) { |
... | ... | |
478 | 490 |
} |
479 | 491 |
|
480 | 492 |
/** |
481 |
* Get all webform components that are defined as a group |
|
493 |
* Get all webform components that are defined as a group.
|
|
482 | 494 |
*/ |
483 | 495 |
function _webform_validation_get_group_types() { |
484 | 496 |
$types = array(); |
... | ... | |
494 | 506 |
* Implements hook_webform_validator_alter(). |
495 | 507 |
*/ |
496 | 508 |
function webform_validation_webform_validator_alter(&$validators) { |
497 |
// Add support for the Select (or Other) module |
|
509 |
// Add support for the Select (or Other) module.
|
|
498 | 510 |
if (module_exists('select_or_other')) { |
499 |
// if this module exists, all select components can now except user input.
|
|
500 |
// Thus we provide those components the same rules as a textfield |
|
511 |
// If this module exists, all select components can now except user input.
|
|
512 |
// Thus we provide those components the same rules as a textfield.
|
|
501 | 513 |
if ($validators) { |
502 | 514 |
foreach ($validators as $validator_name => $validator_info) { |
503 | 515 |
if (in_array('textfield', $validator_info['component_types'])) { |
Formats disponibles : Unified diff
Weekly update of contrib modules