Révision 5c451ca3
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/webform_validation/webform_validation.validators.inc | ||
---|---|---|
24 | 24 |
'numeric' => array( |
25 | 25 |
'name' => t('Numeric values'), |
26 | 26 |
'component_types' => array( |
27 |
'hidden', |
|
27 | 28 |
'number', |
28 | 29 |
'textfield', |
29 |
'hidden', |
|
30 | 30 |
), |
31 | 31 |
'custom_data' => array( |
32 | 32 |
'label' => t('Specify numeric validation range'), |
... | ... | |
39 | 39 |
'min_length' => array( |
40 | 40 |
'name' => t('Minimum length'), |
41 | 41 |
'component_types' => array( |
42 |
'number', |
|
43 |
'textfield', |
|
44 |
'textarea', |
|
45 | 42 |
'email', |
46 | 43 |
'hidden', |
44 |
'number', |
|
45 |
'textarea', |
|
46 |
'textfield', |
|
47 | 47 |
), |
48 | 48 |
'custom_data' => array( |
49 | 49 |
'label' => t('Minimum number of characters'), |
... | ... | |
54 | 54 |
'max_length' => array( |
55 | 55 |
'name' => t('Maximum length'), |
56 | 56 |
'component_types' => array( |
57 |
'number', |
|
58 |
'textfield', |
|
59 |
'textarea', |
|
60 | 57 |
'email', |
61 | 58 |
'hidden', |
59 |
'number', |
|
60 |
'textarea', |
|
61 |
'textfield', |
|
62 | 62 |
), |
63 | 63 |
'custom_data' => array( |
64 | 64 |
'label' => t('Maximum number of characters'), |
... | ... | |
69 | 69 |
'min_words' => array( |
70 | 70 |
'name' => t('Minimum number of words'), |
71 | 71 |
'component_types' => array( |
72 |
'textfield', |
|
73 |
'textarea', |
|
74 | 72 |
'hidden', |
75 | 73 |
'html_textarea', |
74 |
'textarea', |
|
75 |
'textfield', |
|
76 | 76 |
), |
77 | 77 |
'custom_data' => array( |
78 | 78 |
'label' => t('Minimum number of words'), |
... | ... | |
83 | 83 |
'max_words' => array( |
84 | 84 |
'name' => t('Maximum number of words'), |
85 | 85 |
'component_types' => array( |
86 |
'textfield', |
|
87 |
'textarea', |
|
88 | 86 |
'hidden', |
89 | 87 |
'html_textarea', |
88 |
'textarea', |
|
89 |
'textfield', |
|
90 | 90 |
), |
91 | 91 |
'custom_data' => array( |
92 | 92 |
'label' => t('Maximum number of words'), |
... | ... | |
94 | 94 |
), |
95 | 95 |
'description' => t('Verifies that a user-entered value contains at most the specified number of words.'), |
96 | 96 |
), |
97 |
'sum' => array( |
|
98 |
'name' => t('Adds up to'), |
|
99 |
'component_types' => array( |
|
100 |
'number', |
|
101 |
), |
|
102 |
'custom_data' => array( |
|
103 |
'label' => t('Number the fields add up to'), |
|
104 |
'description' => t('Specify the number and the type of comparison. For example:') . theme('item_list', array('items' => array( |
|
105 |
t('Enter "=3" if the components must add up to exactly 3.'), |
|
106 |
t('Enter ">10" if the components must add up to greater than 10.'), |
|
107 |
t('Enter ">=10" if the components must add up to greater than or equal to 10.'), |
|
108 |
t('Enter "<20" if the components must add up to less than 20.'), |
|
109 |
t('Enter "<=20" if the components must add up to less than or equal to 20.'), |
|
110 |
))), |
|
111 |
), |
|
112 |
'description' => t('Require the values of the selected fields to add up to exactly, greater than or equal to, or less than or equal to a specified number.'), |
|
113 |
), |
|
97 | 114 |
'equal' => array( |
98 | 115 |
'name' => t('Equal values'), |
99 | 116 |
'component_types' => array( |
100 |
'number', |
|
101 |
'textfield', |
|
117 |
'date', |
|
102 | 118 |
'email', |
103 |
'select', |
|
104 | 119 |
'hidden', |
120 |
'number', |
|
121 |
'select', |
|
122 |
'textarea', |
|
123 |
'textfield', |
|
124 |
'time', |
|
105 | 125 |
), |
106 | 126 |
'min_components' => 2, |
107 |
'description' => t('Verifies that all specified components contain equal values.'), |
|
127 |
'description' => t('Verifies that all specified components contain equal values. If all components are of type email, they will get case-insensitive comparison.'),
|
|
108 | 128 |
), |
109 | 129 |
'comparison' => array( |
110 | 130 |
'name' => t('Compare two values'), |
... | ... | |
113 | 133 |
'email', |
114 | 134 |
'hidden', |
115 | 135 |
'number', |
116 |
'textfield', |
|
117 | 136 |
'select', |
137 |
'textarea', |
|
138 |
'textfield', |
|
118 | 139 |
'time', |
119 | 140 |
), |
120 | 141 |
'custom_error' => TRUE, |
121 | 142 |
'custom_data' => array( |
122 | 143 |
'label' => t('Comparison operator'), |
123 |
'description' => t('Specify the comparison operator you want to use. Must be one of: >, >=, <, <=. The validator will compare the first component with the second using this operator.'), |
|
144 |
'description' => t('Specify the comparison operator you want to use. Must be one of: >, >=, <, <=. The validator will compare the first component with the second using this operator. If the components are of type email, they will get case-insensitive comparison.'),
|
|
124 | 145 |
), |
125 | 146 |
'min_components' => 2, |
126 | 147 |
'max_components' => 2, |
... | ... | |
129 | 150 |
'unique' => array( |
130 | 151 |
'name' => t('Unique values'), |
131 | 152 |
'component_types' => array( |
132 |
'number', |
|
133 |
'textfield', |
|
153 |
'date', |
|
134 | 154 |
'email', |
135 |
'select', |
|
136 | 155 |
'hidden', |
156 |
'number', |
|
157 |
'select', |
|
158 |
'textarea', |
|
159 |
'textfield', |
|
160 |
'time', |
|
137 | 161 |
), |
138 | 162 |
'min_components' => 2, |
139 |
'description' => t('Verifies that all specified components contain unique values.'),
|
|
163 |
'description' => t('Verifies that none of the specified components contain the same value as another selected component in this submission. (To check that values are unique between submissions, use the unique validation option on the "Edit component" page for that component.) If all components are of type email, they will get case-insensitive comparison.'),
|
|
140 | 164 |
), |
141 | 165 |
'specific_value' => array( |
142 | 166 |
'name' => t('Specific value(s)'), |
143 | 167 |
'negatable' => TRUE, |
144 | 168 |
'component_types' => array( |
169 |
'email', |
|
170 |
'hidden', |
|
145 | 171 |
'number', |
146 | 172 |
'select', |
147 |
'textfield', |
|
148 | 173 |
'textarea', |
149 |
'email', |
|
150 |
'hidden', |
|
174 |
'textfield', |
|
151 | 175 |
), |
152 | 176 |
'custom_error' => TRUE, |
153 | 177 |
'custom_data' => array( |
... | ... | |
161 | 185 |
'name' => t('Default value'), |
162 | 186 |
'negatable' => TRUE, |
163 | 187 |
'component_types' => array( |
164 |
'select', |
|
165 |
'number', |
|
166 |
'textfield', |
|
167 |
'textarea', |
|
168 | 188 |
'email', |
169 | 189 |
'hidden', |
190 |
'number', |
|
191 |
'select', |
|
192 |
'textarea', |
|
193 |
'textfield', |
|
170 | 194 |
), |
171 | 195 |
'custom_error' => TRUE, |
172 | 196 |
'description' => t('Verifies that the user-entered value is the default value for that component. Negate if you want not the default value.'), |
173 | 197 |
), |
174 | 198 |
'someofseveral' => array( |
175 |
'name' => t('At least one or more out of two or more'),
|
|
199 |
'name' => t('Some of several'),
|
|
176 | 200 |
'component_types' => array( |
177 |
'number', |
|
178 |
'textfield', |
|
179 |
'textarea', |
|
201 |
'date', |
|
180 | 202 |
'email', |
203 |
'file', |
|
204 |
'number', |
|
181 | 205 |
'select', |
206 |
'textarea', |
|
207 |
'textfield', |
|
208 |
'time' |
|
182 | 209 |
), |
183 | 210 |
'custom_data' => array( |
184 |
'label' => t('Minimum number to be completed'), |
|
185 |
'description' => t('Specify the minimum number that must be completed. For example, enter 1 if the user must complete at least 1 of the selected components.'), |
|
211 |
'label' => t('Number to be completed'), |
|
212 |
'description' => t('Specify the number that must be completed and the type of comparison. For example:') . theme('item_list', array('items' => array( |
|
213 |
t('Enter ">=1" if the user must complete <b>at least</b> 1 of the selected components.'), |
|
214 |
t('Enter "=3" if the user must complete <b>exactly</b> 3 of the selected components.'), |
|
215 |
t('Enter "<=2" if the user must complete <b>at most</b> 2 of the selected components.'), |
|
216 |
))), |
|
186 | 217 |
), |
187 | 218 |
'min_components' => 2, |
188 |
'description' => t('Requires the user to complete at least one or more of two or more Webform components, such as 3 out of 5 or 1 out of 3.'),
|
|
219 |
'description' => t('Requires the user to complete some number of components out of a group of components. For example, complete at least 2 out of 3, complete at most 4 out of 6, or complete exactly 3 our of 4.'),
|
|
189 | 220 |
), |
190 | 221 |
'select_min' => array( |
191 | 222 |
'name' => t('Minimum number of selections required'), |
... | ... | |
225 | 256 |
'name' => t('Plain text (disallow tags)'), |
226 | 257 |
'negatable' => TRUE, |
227 | 258 |
'component_types' => array( |
228 |
'textfield', |
|
229 |
'textarea', |
|
230 | 259 |
'email', |
231 | 260 |
'hidden', |
261 |
'textarea', |
|
262 |
'textfield', |
|
232 | 263 |
), |
233 | 264 |
'description' => t('Verifies that user-entered data doesn\'t contain HTML tags.'), |
234 | 265 |
), |
... | ... | |
236 | 267 |
'name' => t('Starts with'), |
237 | 268 |
'negatable' => TRUE, |
238 | 269 |
'component_types' => array( |
239 |
'number', |
|
240 |
'textfield', |
|
241 |
'textarea', |
|
242 | 270 |
'email', |
243 | 271 |
'hidden', |
272 |
'number', |
|
273 |
'textarea', |
|
274 |
'textfield', |
|
244 | 275 |
), |
245 | 276 |
'custom_data' => array( |
246 | 277 |
'label' => t('Starts with'), |
... | ... | |
252 | 283 |
'name' => t('Ends with'), |
253 | 284 |
'negatable' => TRUE, |
254 | 285 |
'component_types' => array( |
255 |
'number', |
|
256 |
'textfield', |
|
257 |
'textarea', |
|
258 | 286 |
'email', |
259 | 287 |
'hidden', |
288 |
'number', |
|
289 |
'textarea', |
|
290 |
'textfield', |
|
260 | 291 |
), |
261 | 292 |
'custom_data' => array( |
262 | 293 |
'label' => t('Ends with'), |
... | ... | |
265 | 296 |
'description' => t('Verifies that user-entered data ends with a given string.'), |
266 | 297 |
), |
267 | 298 |
'pattern' => array( |
268 |
'name' => 'Pattern',
|
|
299 |
'name' => t('Pattern'),
|
|
269 | 300 |
'component_types' => array( |
270 |
'textfield', |
|
271 | 301 |
'hidden', |
302 |
'textarea', |
|
303 |
'textfield', |
|
272 | 304 |
), |
273 | 305 |
'custom_error' => TRUE, |
274 | 306 |
'custom_data' => array( |
... | ... | |
284 | 316 |
'name' => t('Regular expression, case-sensitive'), |
285 | 317 |
'negatable' => TRUE, |
286 | 318 |
'component_types' => array( |
287 |
'number', |
|
288 |
'textfield', |
|
289 |
'textarea', |
|
290 | 319 |
'email', |
291 | 320 |
'hidden', |
321 |
'number', |
|
322 |
'textarea', |
|
323 |
'textfield', |
|
292 | 324 |
), |
293 | 325 |
'custom_error' => TRUE, |
294 | 326 |
'custom_data' => array( |
... | ... | |
301 | 333 |
'name' => t('Regular expression, case-insensitive'), |
302 | 334 |
'negatable' => TRUE, |
303 | 335 |
'component_types' => array( |
304 |
'number', |
|
305 |
'textfield', |
|
306 |
'textarea', |
|
307 | 336 |
'email', |
308 | 337 |
'hidden', |
338 |
'number', |
|
339 |
'textarea', |
|
340 |
'textfield', |
|
309 | 341 |
), |
310 | 342 |
'custom_error' => TRUE, |
311 | 343 |
'custom_data' => array( |
... | ... | |
317 | 349 |
'must_be_empty' => array( |
318 | 350 |
'name' => t('Must be empty'), |
319 | 351 |
'component_types' => array( |
352 |
'hidden', |
|
320 | 353 |
'number', |
354 |
'textarea', |
|
321 | 355 |
'textfield', |
322 |
'hidden', |
|
323 | 356 |
), |
324 | 357 |
'description' => t('Verifies that a specified textfield remains empty. Recommended use case: used as an anti-spam measure by hiding the element with CSS.'), |
325 | 358 |
), |
... | ... | |
327 | 360 |
'name' => t('Words blacklist'), |
328 | 361 |
'negatable' => TRUE, |
329 | 362 |
'component_types' => array( |
330 |
'textfield', |
|
331 |
'textarea', |
|
332 | 363 |
'email', |
333 | 364 |
'hidden', |
365 |
'textarea', |
|
366 |
'textfield', |
|
334 | 367 |
), |
335 | 368 |
'custom_error' => TRUE, |
336 | 369 |
'custom_data' => array( |
... | ... | |
343 | 376 |
'name' => t('Must match a username'), |
344 | 377 |
'negatable' => TRUE, |
345 | 378 |
'component_types' => array( |
346 |
'textfield', |
|
347 | 379 |
'hidden', |
380 |
'textfield', |
|
348 | 381 |
), |
349 | 382 |
'description' => t('Validates that user-entered data matches a username.'), |
350 | 383 |
), |
351 | 384 |
'valid_url' => array( |
352 |
'name' => 'Valid URL',
|
|
385 |
'name' => t('Valid URL'),
|
|
353 | 386 |
'negatable' => TRUE, |
354 | 387 |
'component_types' => array( |
355 |
'textfield', |
|
356 | 388 |
'hidden', |
389 |
'textfield', |
|
357 | 390 |
), |
358 | 391 |
'description' => t('Validates that the user-entered data is a valid URL.'), |
359 | 392 |
), |
... | ... | |
361 | 394 |
|
362 | 395 |
if (module_exists('email_verify')) { |
363 | 396 |
$validators['email_verify'] = array( |
364 |
'name' => 'Email Verify',
|
|
397 |
'name' => t('Email Verify'),
|
|
365 | 398 |
'component_types' => array( |
366 | 399 |
'email', |
367 | 400 |
), |
... | ... | |
376 | 409 |
* Implements hook_webform_validation_validate(). |
377 | 410 |
*/ |
378 | 411 |
function webform_validation_webform_validation_validate($validator_name, $items, $components, $rule) { |
379 |
if ($items) { |
|
380 |
// Some components, such as multiple select, send their values as arrays, others don't. |
|
381 |
// Convert all to arrays and write the rules to handle them that way. Remove empty values. |
|
382 |
foreach ($items as $key => $val) { |
|
383 |
$new_values = array(); |
|
384 |
foreach ((array) $val as $val_key => $val_val) { |
|
385 |
if ((string) $val_val !== '') { |
|
386 |
$new_values[$val_key] = $val_val; |
|
387 |
} |
|
412 |
if (!$items) { |
|
413 |
return NULL; |
|
414 |
} |
|
415 |
|
|
416 |
// For certain validators, if all components to be compared are email |
|
417 |
// components, make the values lower-case to avoid case-sensitive comparison. |
|
418 |
if (in_array($validator_name, array('equal', 'comparison', 'unique'))) { |
|
419 |
$all_email = TRUE; |
|
420 |
foreach ($rule['components'] as $component) { |
|
421 |
if ($component['type'] !== 'email') { |
|
422 |
$all_email = FALSE; |
|
423 |
break; |
|
388 | 424 |
} |
389 |
$items[$key] = $new_values; |
|
390 | 425 |
} |
426 |
if ($all_email) { |
|
427 |
$items = array_map('strtolower', $items); |
|
428 |
} |
|
429 |
} |
|
391 | 430 |
|
392 |
$errors = array(); |
|
393 |
switch ($validator_name) { |
|
394 |
case 'numeric': |
|
395 |
$num_range = _webform_numeric_check_data($rule['data']); |
|
396 |
foreach ($items as $key => $val) { |
|
397 |
foreach ($val as $subval) { |
|
398 |
// first check if the value is numeric |
|
399 |
if (is_numeric($subval)) { |
|
400 |
$subval = (float) $subval; |
|
401 |
} |
|
402 |
else { |
|
403 |
$errors[$key] = t('%item is not numeric.', array('%item' => $components[$key]['name'])); |
|
404 |
continue; |
|
405 |
} |
|
431 |
// Some components, such as multiple select, send their values as arrays, others don't. |
|
432 |
// Convert all to arrays and write the rules to handle them that way. Remove empty values. |
|
433 |
foreach ($items as $key => $val) { |
|
434 |
$new_values = array(); |
|
435 |
foreach ((array) $val as $val_key => $val_val) { |
|
436 |
if ((string) $val_val !== '') { |
|
437 |
$new_values[$val_key] = $val_val; |
|
438 |
} |
|
439 |
} |
|
440 |
$items[$key] = $new_values; |
|
441 |
} |
|
406 | 442 |
|
407 |
// now validate the entered numeric value against the validator range settings, if appropriate |
|
408 |
// a. validate min & max |
|
409 |
if (isset($num_range['min']) && isset($num_range['max'])) { |
|
410 |
// validate the min - max range |
|
411 |
if ($subval < $num_range['min'] || $subval > $num_range['max']) { |
|
412 |
$errors[$key] = t('%item is not within the allowed range %range.', array('%item' => $components[$key]['name'], '%range' => str_replace('|' , ' - ', $rule['data']))); |
|
413 |
} |
|
443 |
$errors = array(); |
|
444 |
switch ($validator_name) { |
|
445 |
case 'numeric': |
|
446 |
$num_range = _webform_numeric_check_data($rule['data']); |
|
447 |
foreach ($items as $key => $val) { |
|
448 |
foreach ($val as $subval) { |
|
449 |
// first check if the value is numeric |
|
450 |
if (is_numeric($subval)) { |
|
451 |
$subval = (float) $subval; |
|
452 |
} |
|
453 |
else { |
|
454 |
$errors[$key] = t('%item is not numeric.', array('%item' => $components[$key]['name'])); |
|
455 |
continue; |
|
456 |
} |
|
457 |
|
|
458 |
// now validate the entered numeric value against the validator range settings, if appropriate |
|
459 |
// a. validate min & max |
|
460 |
if (isset($num_range['min']) && isset($num_range['max'])) { |
|
461 |
// validate the min - max range |
|
462 |
if ($subval < $num_range['min'] || $subval > $num_range['max']) { |
|
463 |
$errors[$key] = t('%item is not within the allowed range %range.', array('%item' => $components[$key]['name'], '%range' => str_replace('|' , ' - ', $rule['data']))); |
|
414 | 464 |
} |
415 |
else {
|
|
416 |
// b. validate min
|
|
417 |
if (isset($num_range['min'])) {
|
|
418 |
if ($subval < $num_range['min']) {
|
|
419 |
$errors[$key] = t('%item should be greater than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['min']));
|
|
420 |
}
|
|
465 |
}
|
|
466 |
else {
|
|
467 |
// b. validate min
|
|
468 |
if (isset($num_range['min'])) {
|
|
469 |
if ($subval < $num_range['min']) {
|
|
470 |
$errors[$key] = t('%item should be greater than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['min']));
|
|
421 | 471 |
} |
422 |
// c. validate max
|
|
423 |
if (isset($num_range['max'])) {
|
|
424 |
if ($subval > $num_range['max']) {
|
|
425 |
$errors[$key] = t('%item should be less than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['max']));
|
|
426 |
}
|
|
472 |
}
|
|
473 |
// c. validate max
|
|
474 |
if (isset($num_range['max'])) {
|
|
475 |
if ($subval > $num_range['max']) {
|
|
476 |
$errors[$key] = t('%item should be less than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['max']));
|
|
427 | 477 |
} |
428 | 478 |
} |
429 | 479 |
} |
430 | 480 |
} |
431 |
return $errors;
|
|
432 |
case 'min_length':
|
|
433 |
$min_length = $rule['data'];
|
|
434 |
foreach ($items as $key => $val) {
|
|
435 |
foreach ($val as $subval) {
|
|
436 |
if (drupal_strlen($subval) < $min_length) {
|
|
437 |
$errors[$key] = t('%item should be at least %num characters long.', array('%item' => $components[$key]['name'], '%num' => $min_length));
|
|
438 |
}
|
|
481 |
}
|
|
482 |
return $errors;
|
|
483 |
case 'min_length':
|
|
484 |
$min_length = $rule['data'];
|
|
485 |
foreach ($items as $key => $val) {
|
|
486 |
foreach ($val as $subval) {
|
|
487 |
if (drupal_strlen($subval) < $min_length) {
|
|
488 |
$errors[$key] = t('%item should be at least %num characters long.', array('%item' => $components[$key]['name'], '%num' => $min_length));
|
|
439 | 489 |
} |
440 | 490 |
} |
441 |
return $errors;
|
|
442 |
case 'max_length':
|
|
443 |
$max_length = $rule['data'];
|
|
444 |
foreach ($items as $key => $val) {
|
|
445 |
foreach ($val as $subval) {
|
|
446 |
if (drupal_strlen($subval) > $max_length) {
|
|
447 |
$errors[$key] = t('%item should be at most %num characters long.', array('%item' => $components[$key]['name'], '%num' => $max_length));
|
|
448 |
}
|
|
491 |
}
|
|
492 |
return $errors;
|
|
493 |
case 'max_length':
|
|
494 |
$max_length = $rule['data'];
|
|
495 |
foreach ($items as $key => $val) {
|
|
496 |
foreach ($val as $subval) {
|
|
497 |
if (drupal_strlen($subval) > $max_length) {
|
|
498 |
$errors[$key] = t('%item should be at most %num characters long.', array('%item' => $components[$key]['name'], '%num' => $max_length));
|
|
449 | 499 |
} |
450 | 500 |
} |
451 |
return $errors;
|
|
452 |
case 'min_words':
|
|
453 |
$min_words = $rule['data'];
|
|
454 |
foreach ($items as $key => $val) {
|
|
455 |
foreach ($val as $subval) {
|
|
456 |
if (_webform_validation_count_words($subval) < $min_words) {
|
|
457 |
$error = format_plural($min_words, '%item should be at least 1 word long.', '%item should be at least @count words long.', array('%item' => $components[$key]['name']));
|
|
458 |
$errors[$key] = $error;
|
|
459 |
}
|
|
501 |
}
|
|
502 |
return $errors;
|
|
503 |
case 'min_words':
|
|
504 |
$min_words = $rule['data'];
|
|
505 |
foreach ($items as $key => $val) {
|
|
506 |
foreach ($val as $subval) {
|
|
507 |
if (_webform_validation_count_words($subval) < $min_words) {
|
|
508 |
$error = format_plural($min_words, '%item should be at least 1 word long.', '%item should be at least @count words long.', array('%item' => $components[$key]['name']));
|
|
509 |
$errors[$key] = $error;
|
|
460 | 510 |
} |
461 | 511 |
} |
462 |
return $errors;
|
|
463 |
case 'max_words':
|
|
464 |
$max_words = $rule['data'];
|
|
465 |
foreach ($items as $key => $val) {
|
|
466 |
foreach ($val as $subval) {
|
|
467 |
if (_webform_validation_count_words($subval) > $max_words) {
|
|
468 |
$error = format_plural($max_words, '%item should be at most 1 word long.', '%item should be at most @count words long.', array('%item' => $components[$key]['name']));
|
|
469 |
$errors[$key] = $error;
|
|
470 |
}
|
|
512 |
}
|
|
513 |
return $errors;
|
|
514 |
case 'max_words':
|
|
515 |
$max_words = $rule['data'];
|
|
516 |
foreach ($items as $key => $val) {
|
|
517 |
foreach ($val as $subval) {
|
|
518 |
if (_webform_validation_count_words($subval) > $max_words) {
|
|
519 |
$error = format_plural($max_words, '%item should be at most 1 word long.', '%item should be at most @count words long.', array('%item' => $components[$key]['name']));
|
|
520 |
$errors[$key] = $error;
|
|
471 | 521 |
} |
472 | 522 |
} |
473 |
return $errors; |
|
474 |
case 'equal': |
|
475 |
$first_entry_key = key($items); |
|
476 |
$first_entry = array_shift($items); |
|
477 |
foreach ($items as $key => $val) { |
|
478 |
if ($val !== $first_entry) { |
|
479 |
$errors[$key] = t('%item_checked does not match %item_first.', array('%item_checked' => $components[$key]['name'], '%item_first' => $components[$first_entry_key]['name'])); |
|
523 |
} |
|
524 |
return $errors; |
|
525 |
case 'sum': |
|
526 |
// Get the numbers to sum. |
|
527 |
$sum = array(); |
|
528 |
foreach ($items as $item) { |
|
529 |
if (isset($item[0])) { |
|
530 |
$sum[] = $item[0]; |
|
531 |
} |
|
532 |
} |
|
533 |
// If none of the components are completed, don't run this validator. |
|
534 |
if (!count($sum)) { |
|
535 |
return array(); |
|
536 |
} |
|
537 |
$sum = array_sum($sum); |
|
538 |
|
|
539 |
// Number being compared with. |
|
540 |
$compare_number = (float) preg_replace('/^[^0-9]+/', '', $rule['data']); |
|
541 |
|
|
542 |
// Parse the comparision operator and do comparison. |
|
543 |
module_load_include('inc', 'webform', 'includes/webform.conditionals'); |
|
544 |
$error = FALSE; |
|
545 |
if (substr($rule['data'], 0, 2) === '<=') { |
|
546 |
if (!(webform_conditional_compare_floats($sum, $compare_number) <= 0)) { |
|
547 |
$error = t('less than or equal to'); |
|
548 |
} |
|
549 |
} |
|
550 |
elseif (substr($rule['data'], 0, 1) === '<') { |
|
551 |
if (!(webform_conditional_compare_floats($sum, $compare_number) < 0)) { |
|
552 |
$error = t('less than'); |
|
553 |
} |
|
554 |
} |
|
555 |
elseif (substr($rule['data'], 0, 2) === '>=') { |
|
556 |
if (!(webform_conditional_compare_floats($sum, $compare_number) >= 0)) { |
|
557 |
$error = t('greater than or equal to'); |
|
558 |
} |
|
559 |
} |
|
560 |
elseif (substr($rule['data'], 0, 1) === '>') { |
|
561 |
if (!(webform_conditional_compare_floats($sum, $compare_number) > 0)) { |
|
562 |
$error = t('greater than'); |
|
563 |
} |
|
564 |
} |
|
565 |
else { |
|
566 |
if (!(webform_conditional_compare_floats($sum, $compare_number) === 0)) { |
|
567 |
$error = t('exactly'); |
|
568 |
} |
|
569 |
} |
|
570 |
// Set error if needed. |
|
571 |
if ($error) { |
|
572 |
$keys = array_keys($items); |
|
573 |
$errors[$keys[0]] = t('These items must add up to %verb %compare_number:', array('%verb' => $error, '%compare_number' => $compare_number)) . theme('item_list', array('items' => _webform_validation_get_names_of_rule_components($rule))); |
|
574 |
} |
|
575 |
return $errors; |
|
576 |
case 'equal': |
|
577 |
$first_entry_key = key($items); |
|
578 |
$first_entry = array_shift($items); |
|
579 |
foreach ($items as $key => $val) { |
|
580 |
if ($val !== $first_entry) { |
|
581 |
$errors[$key] = t('%item_checked does not match %item_first.', array('%item_checked' => $components[$key]['name'], '%item_first' => $components[$first_entry_key]['name'])); |
|
582 |
} |
|
583 |
} |
|
584 |
return $errors; |
|
585 |
case 'comparison': |
|
586 |
foreach (array(1, 2) as $count) { |
|
587 |
$entry[$count]['key'] = key($items); |
|
588 |
$value = array_shift($items); |
|
589 |
if ($components[$entry[$count]['key']]['type'] === 'date') { |
|
590 |
if (checkdate((int) $value['month'], (int) $value['day'], (int) $value['year'])) { |
|
591 |
$entry[$count]['value'] = date('Y-m-d', mktime(0, 0, 0, (int) $value['month'], (int) $value['day'], (int) $value['year'])); |
|
592 |
} |
|
593 |
else { |
|
594 |
$entry[$count]['value'] = NULL; |
|
480 | 595 |
} |
481 | 596 |
} |
482 |
return $errors; |
|
483 |
case 'comparison': |
|
484 |
foreach (array(1, 2) as $count) { |
|
485 |
$entry[$count]['key'] = key($items); |
|
486 |
$value = array_shift($items); |
|
487 |
if ($components[$entry[$count]['key']]['type'] === 'date') { |
|
488 |
if (checkdate((int) $value['month'], (int) $value['day'], (int) $value['year'])) { |
|
489 |
$entry[$count]['value'] = date('Y-m-d', mktime(0, 0, 0, (int) $value['month'], (int) $value['day'], (int) $value['year'])); |
|
490 |
} |
|
491 |
else { |
|
492 |
$entry[$count]['value'] = NULL; |
|
493 |
} |
|
597 |
elseif ($components[$entry[$count]['key']]['type'] === 'time') { |
|
598 |
$time = $value['hour'] . ':' . $value['minute']; |
|
599 |
if (!empty($value['ampm'])) { |
|
600 |
$time .= ' ' . $value['ampm']; |
|
494 | 601 |
} |
495 |
elseif ($components[$entry[$count]['key']]['type'] === 'time') { |
|
496 |
$time = $value['hour'] . ':' . $value['minute']; |
|
497 |
if (!empty($value['ampm'])) { |
|
498 |
$time .= ' ' . $value['ampm']; |
|
499 |
} |
|
500 |
$time = strtotime($time); |
|
501 |
if ($time) { |
|
502 |
$entry[$count]['value'] = date('H:i', $time); |
|
503 |
} |
|
504 |
else { |
|
505 |
$entry[$count]['value'] = NULL; |
|
506 |
} |
|
602 |
$time = strtotime($time); |
|
603 |
if ($time) { |
|
604 |
$entry[$count]['value'] = date('H:i', $time); |
|
507 | 605 |
} |
508 | 606 |
else { |
509 |
$entry[$count]['value'] = _webform_validation_flatten_array($value);
|
|
607 |
$entry[$count]['value'] = NULL;
|
|
510 | 608 |
} |
511 | 609 |
} |
512 |
|
|
513 |
// Don't validate if either are empty. |
|
514 |
if (empty($entry[1]['value']) || empty($entry[2]['value'])) { |
|
515 |
return; |
|
610 |
else { |
|
611 |
$entry[$count]['value'] = _webform_validation_flatten_array($value); |
|
516 | 612 |
} |
613 |
} |
|
517 | 614 |
|
518 |
$error = FALSE; |
|
519 |
switch ($rule['data']) { |
|
520 |
case '>': |
|
521 |
if (!($entry[1]['value'] > $entry[2]['value'])) { |
|
522 |
$error = TRUE; |
|
523 |
} |
|
524 |
break; |
|
525 |
case '>=': |
|
526 |
if (!($entry[1]['value'] >= $entry[2]['value'])) { |
|
527 |
$error = TRUE; |
|
528 |
} |
|
529 |
break; |
|
530 |
case '<': |
|
531 |
if (!($entry[1]['value'] < $entry[2]['value'])) { |
|
532 |
$error = TRUE; |
|
533 |
} |
|
534 |
break; |
|
535 |
case '<=': |
|
536 |
if (!($entry[1]['value'] <= $entry[2]['value'])) { |
|
537 |
$error = TRUE; |
|
538 |
} |
|
539 |
break; |
|
540 |
} |
|
615 |
// Don't validate if either are empty. |
|
616 |
if ((string) $entry[1]['value'] === '' || (string) $entry[2]['value'] === '') { |
|
617 |
return array(); |
|
618 |
} |
|
541 | 619 |
|
542 |
if ($error) { |
|
543 |
$errors[$entry[2]['key']] = _webform_validation_i18n_error_message($rule); |
|
544 |
} |
|
545 |
return $errors; |
|
546 |
case 'unique': |
|
547 |
foreach ($items as $key => $val) { |
|
548 |
$items[$key] = _webform_validation_flatten_array($val); |
|
549 |
} |
|
550 |
// now we count how many times each value appears, and find out which values appear more than once |
|
551 |
$items_count = array_count_values(array_map('drupal_strtolower', array_map('trim', $items))); |
|
552 |
$doubles = array_filter($items_count, create_function('$x', 'return $x > 1;')); |
|
553 |
foreach ($items as $key => $val) { |
|
554 |
if (in_array(drupal_strtolower($val), array_keys($doubles))) { |
|
555 |
$errors[$key] = t('The value of %item is not unique.', array('%item' => $components[$key]['name'])); |
|
620 |
$error = FALSE; |
|
621 |
switch ($rule['data']) { |
|
622 |
case '>': |
|
623 |
if (!($entry[1]['value'] > $entry[2]['value'])) { |
|
624 |
$error = TRUE; |
|
556 | 625 |
} |
626 |
break; |
|
627 |
case '>=': |
|
628 |
if (!($entry[1]['value'] >= $entry[2]['value'])) { |
|
629 |
$error = TRUE; |
|
630 |
} |
|
631 |
break; |
|
632 |
case '<': |
|
633 |
if (!($entry[1]['value'] < $entry[2]['value'])) { |
|
634 |
$error = TRUE; |
|
635 |
} |
|
636 |
break; |
|
637 |
case '<=': |
|
638 |
if (!($entry[1]['value'] <= $entry[2]['value'])) { |
|
639 |
$error = TRUE; |
|
640 |
} |
|
641 |
break; |
|
642 |
} |
|
643 |
|
|
644 |
if ($error) { |
|
645 |
$errors[$entry[2]['key']] = _webform_validation_i18n_error_message($rule); |
|
646 |
} |
|
647 |
return $errors; |
|
648 |
case 'unique': |
|
649 |
foreach ($items as $key => $val) { |
|
650 |
$items[$key] = _webform_validation_flatten_array($val); |
|
651 |
} |
|
652 |
// now we count how many times each value appears, and find out which values appear more than once |
|
653 |
$items_count = array_count_values(array_map('drupal_strtolower', array_map('trim', $items))); |
|
654 |
$doubles = array_filter($items_count, create_function('$x', 'return $x > 1;')); |
|
655 |
foreach ($items as $key => $val) { |
|
656 |
if (in_array(drupal_strtolower($val), array_keys($doubles))) { |
|
657 |
$errors[$key] = t('The value of %item is not unique.', array('%item' => $components[$key]['name'])); |
|
557 | 658 |
} |
558 |
return $errors; |
|
559 |
case 'specific_value': |
|
560 |
$specific_values = explode(',', $rule['data']); |
|
561 |
$specific_values = array_map('trim', $specific_values); |
|
562 |
foreach ($items as $key => $val) { |
|
563 |
// Selects: Fail if at least one checked and at least one not in the allowed values. |
|
564 |
$val = array_filter($val); |
|
565 |
$test = count($val) && count(array_diff($val, $specific_values)); |
|
566 |
_webform_validation_test($errors, $key, $rule, $test); |
|
567 |
} |
|
568 |
return $errors; |
|
569 |
case 'default_value': |
|
570 |
foreach ($items as $key => $val) { |
|
571 |
$val = _webform_validation_flatten_array($val); |
|
572 |
$test = $val != $components[$key]['value']; |
|
573 |
_webform_validation_test($errors, $key, $rule, $test); |
|
659 |
} |
|
660 |
return $errors; |
|
661 |
case 'specific_value': |
|
662 |
$specific_values = explode(',', $rule['data']); |
|
663 |
$specific_values = array_map('trim', $specific_values); |
|
664 |
foreach ($items as $key => $val) { |
|
665 |
// Selects: Fail if at least one checked and at least one not in the allowed values. |
|
666 |
$val = array_filter($val); |
|
667 |
$test = count($val) && count(array_diff($val, $specific_values)); |
|
668 |
_webform_validation_test($errors, $key, $rule, $test); |
|
669 |
} |
|
670 |
return $errors; |
|
671 |
case 'default_value': |
|
672 |
foreach ($items as $key => $val) { |
|
673 |
$val = _webform_validation_flatten_array($val); |
|
674 |
$test = $val != $components[$key]['value']; |
|
675 |
_webform_validation_test($errors, $key, $rule, $test); |
|
676 |
} |
|
677 |
return $errors; |
|
678 |
case 'someofseveral': |
|
679 |
// Determine the number of components completed. |
|
680 |
foreach ($items as $key => $val) { |
|
681 |
$items[$key] = _webform_validation_flatten_array($val); |
|
682 |
$items[$key] = (bool) strlen($items[$key]); |
|
683 |
} |
|
684 |
$number_completed = count(array_filter($items)); |
|
685 |
|
|
686 |
// Number being compared with. |
|
687 |
$compare_number = (int) preg_replace('/[^0-9]+/', '', $rule['data']); |
|
688 |
if ($compare_number < 1) { |
|
689 |
$compare_number = 1; |
|
690 |
} |
|
691 |
elseif ($compare_number > count($rule['components'])) { |
|
692 |
$compare_number = count($rule['components']); |
|
693 |
} |
|
694 |
|
|
695 |
// Parse the comparision operator and do comparison. |
|
696 |
$error = FALSE; |
|
697 |
if (substr($rule['data'], 0, 1) === '=') { |
|
698 |
if (!($number_completed === $compare_number)) { |
|
699 |
$error = t('exactly'); |
|
574 | 700 |
} |
575 |
return $errors; |
|
576 |
case 'someofseveral': |
|
577 |
foreach ($items as $key => $val) { |
|
578 |
$items[$key] = _webform_validation_flatten_array($val); |
|
579 |
$items[$key] = (bool) strlen($items[$key]); |
|
701 |
} |
|
702 |
elseif (substr($rule['data'], 0, 2) === '<=') { |
|
703 |
if (!($number_completed <= $compare_number)) { |
|
704 |
$error = t('at most'); |
|
580 | 705 |
} |
581 |
$min_selections = isset($rule['data']) ? ((int) $rule['data']) : 1; |
|
582 |
if (count(array_filter($items)) < $min_selections) { |
|
583 |
$keys = array_keys($items); |
|
584 |
$names = array(); |
|
585 |
foreach ($keys as $value) { |
|
586 |
$names[] = _webform_filter_xss($components[$value]['name']); |
|
587 |
} |
|
588 |
return array($keys[0] => t('You must complete at least %min_selections of these items:', array('%min_selections' => $min_selections)) . theme('item_list', array('items' => $names))); |
|
706 |
} |
|
707 |
else { |
|
708 |
if (!($number_completed >= $compare_number)) { |
|
709 |
$error = t('at least'); |
|
589 | 710 |
} |
590 |
return; |
|
591 |
case 'select_min': |
|
592 |
$min_selections = $rule['data']; |
|
593 |
foreach ($items as $key => $val) { |
|
594 |
if (count(array_filter($val, '_webform_validation_check_false')) < $min_selections) { |
|
595 |
$errors[$key] = t('Please select at least %num options for %item.', array('%num' => $min_selections, '%item' => $components[$key]['name'])); |
|
596 |
} |
|
711 |
} |
|
712 |
|
|
713 |
// Set error if needed. |
|
714 |
if ($error) { |
|
715 |
$keys = array_keys($items); |
|
716 |
$errors[$keys[0]] = t('You must complete %verb %compare_number of these items:', array('%verb' => $error, '%compare_number' => $compare_number)) . theme('item_list', array('items' => _webform_validation_get_names_of_rule_components($rule))); |
|
717 |
} |
|
718 |
return $errors; |
|
719 |
case 'select_min': |
|
720 |
$min_selections = $rule['data']; |
|
721 |
foreach ($items as $key => $val) { |
|
722 |
if (count(array_filter($val, '_webform_validation_check_false')) < $min_selections) { |
|
723 |
$errors[$key] = t('Please select at least %num options for %item.', array('%num' => $min_selections, '%item' => $components[$key]['name'])); |
|
597 | 724 |
} |
598 |
return $errors;
|
|
599 |
case 'select_max':
|
|
600 |
$max_selections = $rule['data'];
|
|
601 |
foreach ($items as $key => $val) {
|
|
602 |
if (count(array_filter($val, '_webform_validation_check_false')) > $max_selections) {
|
|
603 |
$errors[$key] = t('Please select maximum %num options for %item.', array('%num' => $max_selections, '%item' => $components[$key]['name']));
|
|
604 |
}
|
|
725 |
}
|
|
726 |
return $errors;
|
|
727 |
case 'select_max':
|
|
728 |
$max_selections = $rule['data'];
|
|
729 |
foreach ($items as $key => $val) {
|
|
730 |
if (count(array_filter($val, '_webform_validation_check_false')) > $max_selections) {
|
|
731 |
$errors[$key] = t('Please select maximum %num options for %item.', array('%num' => $max_selections, '%item' => $components[$key]['name']));
|
|
605 | 732 |
} |
606 |
return $errors; |
|
607 |
case 'select_exact': |
|
608 |
$allowed_selections = $rule['data']; |
|
609 |
foreach ($items as $key => $val) { |
|
610 |
$error_strings = array( |
|
611 |
'regular' => 'Please select %num options for %item.', |
|
612 |
'negated' => 'Please do not select %num options for %item.', |
|
613 |
); |
|
614 |
$error_vars = array('%num' => $allowed_selections, '%item' => $components[$key]['name']); |
|
615 |
$test = count(array_filter($val, '_webform_validation_check_false')) != $allowed_selections; |
|
733 |
} |
|
734 |
return $errors; |
|
735 |
case 'select_exact': |
|
736 |
$allowed_selections = $rule['data']; |
|
737 |
foreach ($items as $key => $val) { |
|
738 |
$error_strings = array( |
|
739 |
'regular' => 'Please select %num options for %item.', |
|
740 |
'negated' => 'Please do not select %num options for %item.', |
|
741 |
); |
|
742 |
$error_vars = array('%num' => $allowed_selections, '%item' => $components[$key]['name']); |
|
743 |
$test = count(array_filter($val, '_webform_validation_check_false')) != $allowed_selections; |
|
744 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars); |
|
745 |
} |
|
746 |
return $errors; |
|
747 |
case 'plain_text': |
|
748 |
foreach ($items as $key => $val) { |
|
749 |
$error_strings = array( |
|
750 |
'regular' => '%item only allows the use of plain text.', |
|
751 |
'negated' => '%item must contain HTML tags.', |
|
752 |
); |
|
753 |
$error_vars = array('%item' => $components[$key]['name']); |
|
754 |
foreach ($val as $subval) { |
|
755 |
$test = strcmp($subval, strip_tags($subval)); |
|
616 | 756 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars); |
617 | 757 |
} |
618 |
return $errors; |
|
619 |
case 'plain_text': |
|
620 |
foreach ($items as $key => $val) { |
|
621 |
$error_strings = array( |
|
622 |
'regular' => '%item only allows the use of plain text.', |
|
623 |
'negated' => '%item must contain HTML tags.', |
|
624 |
); |
|
625 |
$error_vars = array('%item' => $components[$key]['name']); |
|
626 |
foreach ($val as $subval) { |
|
627 |
$test = strcmp($subval, strip_tags($subval)); |
|
628 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars); |
|
629 |
} |
|
630 |
} |
|
631 |
return $errors; |
|
632 |
case 'starts_with': |
|
633 |
case 'ends_with': |
|
634 |
$pattern = _webform_validation_preg_quote($rule['data'], '/'); |
|
635 |
if ($validator_name === 'starts_with') { |
|
636 |
$pattern = '^' . $pattern; |
|
637 |
$verb = t('start'); |
|
758 |
} |
|
759 |
return $errors; |
|
760 |
case 'starts_with': |
|
761 |
case 'ends_with': |
|
762 |
$pattern = preg_quote($rule['data'], '/'); |
|
763 |
if ($validator_name === 'starts_with') { |
|
764 |
$pattern = '^' . $pattern; |
|
765 |
$verb = t('start'); |
|
766 |
} |
|
767 |
else { |
|
768 |
$pattern .= '$'; |
|
769 |
$verb = t('end'); |
|
770 |
} |
|
771 |
$pattern = '/' . $pattern . '/'; |
|
772 |
|
|
773 |
foreach ($items as $key => $val) { |
|
774 |
$error_strings = array( |
|
775 |
'regular' => '%item must %verb with "%string".', |
|
776 |
'negated' => '%item must not %verb with "%string".', |
|
777 |
); |
|
778 |
$error_vars = array('%item' => $components[$key]['name'], '%verb' => $verb, '%string' => $rule['data']); |
|
779 |
foreach ($val as $subval) { |
|
780 |
$test = !preg_match($pattern, $subval); |
|
781 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars); |
|
638 | 782 |
} |
639 |
else { |
|
640 |
$pattern .= '$'; |
|
641 |
$verb = t('end'); |
|
783 |
} |
|
784 |
return $errors; |
|
785 |
case 'pattern': |
|
786 |
$pattern = preg_quote($rule['data'], '/'); |
|
787 |
$pattern = str_replace('@', '[a-zA-Z]', $pattern); |
|
788 |
$pattern = str_replace('#', '[0-9]', $pattern); |
|
789 |
$pattern = '(' . $pattern . ')'; |
|
790 |
// Un-escape "|" operator. |
|
791 |
$pattern = preg_replace('/(\\s*)(\\\\)(\\|)(\\s*)/', ')|(', $pattern); |
|
792 |
foreach ($items as $key => $val) { |
|
793 |
foreach ($val as $subval) { |
|
794 |
$test = !preg_match('/^(' . $pattern . ')$/', $subval); |
|
795 |
_webform_validation_test($errors, $key, $rule, $test); |
|
642 | 796 |
} |
643 |
$pattern = '/' . $pattern . '/'; |
|
644 |
|
|
645 |
foreach ($items as $key => $val) { |
|
646 |
$error_strings = array( |
|
647 |
'regular' => '%item must %verb with "%string".', |
|
648 |
'negated' => '%item must not %verb with "%string".', |
|
649 |
); |
|
650 |
$error_vars = array('%item' => $components[$key]['name'], '%verb' => $verb, '%string' => $rule['data']); |
|
651 |
foreach ($val as $subval) { |
|
652 |
$test = !preg_match($pattern, $subval); |
|
653 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars); |
|
797 |
} |
|
798 |
return $errors; |
|
799 |
case 'regex': |
|
800 |
case 'regexi': |
|
801 |
mb_regex_encoding('UTF-8'); |
|
802 |
$regex = $rule['data']; |
|
803 |
foreach ($items as $key => $val) { |
|
804 |
foreach ($val as $subval) { |
|
805 |
if ($validator_name === 'regexi') { |
|
806 |
$match = (bool) mb_eregi($regex, $subval); |
|
654 | 807 |
} |
655 |
} |
|
656 |
return $errors; |
|
657 |
case 'pattern': |
|
658 |
$pattern = _webform_validation_preg_quote($rule['data'], '/'); |
|
659 |
$pattern = str_replace('@', '[a-zA-Z]', $pattern); |
|
660 |
$pattern = str_replace('#', '[0-9]', $pattern); |
|
661 |
$pattern = '(' . $pattern . ')'; |
|
662 |
// Un-escape "|" operator. |
|
663 |
$pattern = preg_replace('/(\\s*)(\\\\)(\\|)(\\s*)/', ')|(', $pattern); |
|
664 |
foreach ($items as $key => $val) { |
|
665 |
foreach ($val as $subval) { |
|
666 |
$test = !preg_match('/^(' . $pattern . ')$/', $subval); |
|
667 |
_webform_validation_test($errors, $key, $rule, $test); |
|
808 |
else { |
|
809 |
$match = (bool) mb_ereg($regex, $subval); |
|
668 | 810 |
} |
811 |
$test = !$match; |
|
812 |
_webform_validation_test($errors, $key, $rule, $test); |
|
669 | 813 |
} |
670 |
return $errors; |
|
671 |
case 'regex': |
|
672 |
case 'regexi': |
|
673 |
mb_regex_encoding('UTF-8'); |
|
674 |
$regex = $rule['data']; |
|
675 |
foreach ($items as $key => $val) { |
|
676 |
foreach ($val as $subval) { |
|
677 |
if ($validator_name === 'regexi') { |
|
678 |
$match = (bool) mb_eregi($regex, $subval); |
|
679 |
} |
|
680 |
else { |
|
681 |
$match = (bool) mb_ereg($regex, $subval); |
|
682 |
} |
|
683 |
$test = !$match; |
|
684 |
_webform_validation_test($errors, $key, $rule, $test); |
|
685 |
} |
|
814 |
} |
|
815 |
return $errors; |
|
816 |
case 'must_be_empty': |
|
817 |
foreach ($items as $key => $val) { |
|
818 |
if (count($val) !== 0) { |
|
819 |
$errors[$key] = t('%item does not contain the correct data.', array('%item' => $components[$key]['name'])); |
|
686 | 820 |
} |
687 |
return $errors; |
|
688 |
case 'must_be_empty': |
|
689 |
foreach ($items as $key => $val) { |
|
690 |
if (count($val) !== 0) { |
|
691 |
$errors[$key] = t('%item does not contain the correct data.', array('%item' => $components[$key]['name'])); |
|
692 |
} |
|
821 |
} |
|
822 |
return $errors; |
|
823 |
case 'blacklist': |
|
824 |
$blacklist = preg_quote($rule['data'], '/'); |
|
825 |
$blacklist = explode(',', $blacklist); |
|
826 |
$blacklist = array_map('trim', $blacklist); |
|
827 |
$blacklist = '/\b(' . implode('|', $blacklist) . ')\b/i'; |
|
828 |
foreach ($items as $key => $val) { |
|
829 |
foreach ($val as $subval) { |
|
830 |
$test = preg_match($blacklist, $subval); |
|
831 |
_webform_validation_test($errors, $key, $rule, $test); |
|
693 | 832 |
} |
694 |
return $errors; |
|
695 |
case 'blacklist': |
|
696 |
$blacklist = _webform_validation_preg_quote($rule['data'], '/'); |
|
697 |
$blacklist = explode(',', $blacklist); |
|
698 |
$blacklist = array_map('trim', $blacklist); |
|
699 |
$blacklist = '/\b(' . implode('|', $blacklist) . ')\b/i'; |
|
700 |
foreach ($items as $key => $val) { |
|
701 |
foreach ($val as $subval) { |
|
702 |
$test = preg_match($blacklist, $subval); |
|
703 |
_webform_validation_test($errors, $key, $rule, $test); |
|
704 |
} |
|
833 |
} |
|
834 |
return $errors; |
|
835 |
case 'username': |
|
836 |
foreach ($items as $key => $val) { |
|
837 |
$error_strings = array( |
|
838 |
'regular' => 'The %item field does not match an active username.', |
|
839 |
'negated' => 'The %item field matches an active username.', |
|
840 |
); |
|
841 |
$error_vars = array('%item' => $components[$key]['name']); |
|
842 |
foreach ($val as $subval) { |
|
843 |
$test = !db_query_range('SELECT 1 FROM {users} WHERE name = :username AND status = 1', 0, 1, array(':username' => $subval))->fetchField(); |
|
844 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars); |
|
705 | 845 |
} |
706 |
return $errors;
|
|
707 |
case 'username':
|
|
708 |
foreach ($items as $key => $val) {
|
|
709 |
$error_strings = array(
|
|
710 |
'regular' => 'The %item field does not match an active username.',
|
|
711 |
'negated' => 'The %item field matches an active username.',
|
|
712 |
);
|
|
713 |
$error_vars = array('%item' => $components[$key]['name']);
|
|
714 |
foreach ($val as $subval) {
|
|
715 |
$test = !db_query_range('SELECT 1 FROM {users} WHERE name = :username AND status = 1', 0, 1, array(':username' => $subval))->fetchField();
|
|
716 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
|
|
717 |
}
|
|
846 |
}
|
|
847 |
return $errors;
|
|
848 |
case 'valid_url':
|
|
849 |
foreach ($items as $key => $val) {
|
|
850 |
$error_strings = array(
|
|
851 |
'regular' => '%item does not appear to be a valid URL.',
|
|
852 |
'negated' => '%item must not be a valid URL.',
|
|
853 |
); |
|
854 |
$error_vars = array('%item' => $components[$key]['name']);
|
|
855 |
foreach ($val as $subval) {
|
|
856 |
$test = !valid_url($subval, TRUE);
|
|
857 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
|
|
718 | 858 |
} |
719 |
return $errors; |
|
720 |
case 'valid_url': |
|
859 |
} |
|
860 |
return $errors; |
|
861 |
case 'email_verify': |
|
862 |
if (module_exists('email_verify')) { |
|
721 | 863 |
foreach ($items as $key => $val) { |
722 |
$error_strings = array( |
|
723 |
'regular' => '%item does not appear to be a valid URL.', |
|
724 |
'negated' => '%item must not be a valid URL.', |
|
725 |
); |
|
726 |
$error_vars = array('%item' => $components[$key]['name']); |
|
727 | 864 |
foreach ($val as $subval) { |
728 |
$test = !valid_url($subval, TRUE); |
|
729 |
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars); |
|
730 |
} |
|
731 |
} |
|
732 |
return $errors; |
|
733 |
case 'email_verify': |
|
734 |
if (module_exists('email_verify')) { |
|
735 |
foreach ($items as $key => $val) { |
|
736 |
foreach ($val as $subval) { |
|
737 |
$error = email_verify_check($subval); |
|
738 |
if ($error) { |
|
739 |
$errors[$key] = $error; |
|
740 |
} |
|
865 |
$error = email_verify_check($subval); |
|
866 |
if ($error) { |
|
867 |
$errors[$key] = $error; |
|
741 | 868 |
} |
742 | 869 |
} |
743 | 870 |
} |
744 |
return $errors;
|
|
745 |
}
|
|
871 |
}
|
|
872 |
return $errors;
|
|
746 | 873 |
} |
747 | 874 |
} |
748 | 875 |
|
876 |
/** |
|
877 |
* Return an array of the names of the components in a validation rule. |
|
878 |
* |
|
879 |
* @param $rule array |
|
880 |
* Array of information about a validation rule. |
|
881 |
* |
|
882 |
* @return array |
|
883 |
* Array of the filtered names of the components in $rule. |
|
884 |
*/ |
|
885 |
function _webform_validation_get_names_of_rule_components(array $rule) { |
|
886 |
$names = array(); |
|
887 |
foreach ($rule['components'] as $component) { |
|
888 |
$names[] = _webform_filter_xss($component['name']); |
|
889 |
} |
|
890 |
return $names; |
|
891 |
} |
|
892 |
|
|
749 | 893 |
/** |
750 | 894 |
* Helper function to negate validation rules as needed and create the correct error message. |
751 | 895 |
*/ |
... | ... | |
781 | 925 |
return str_word_count($val); |
782 | 926 |
} |
783 | 927 |
|
784 |
/** |
|
785 |
* Fixed version of preg_quote() to work around PHP bug #47229. Remove when |
|
786 |
* PHP_VERSION < 5.3 is no longer supported. |
|
787 |
*/ |
|
788 |
function _webform_validation_preg_quote($str, $delimiter = NULL) { |
|
789 |
$str = preg_quote($str, $delimiter); |
|
790 |
if (PHP_VERSION < 5.3) { |
|
791 |
$str = str_replace('-', '\-', $str); |
|
792 |
} |
|
793 |
return $str; |
|
794 |
} |
|
795 |
|
|
796 | 928 |
/** |
797 | 929 |
* Helper function to deal with submitted values that are arrays (for example, multiple select component). |
798 | 930 |
* We flatten the array as a comma-separated list to do the comparison. |
... | ... | |
806 | 938 |
} |
807 | 939 |
|
808 | 940 |
/** |
809 |
* Get a list of validator definitions |
|
941 |
* Get a list of validator definitions. |
|
942 |
* |
|
943 |
* @return |
|
944 |
* Array of information about validators. |
|
810 | 945 |
*/ |
811 | 946 |
function webform_validation_get_validators() { |
812 | 947 |
$validators = module_invoke_all('webform_validation_validators'); |
813 |
// let modules use hook_webform_validator_alter($validators) to change validator settings
|
|
948 |
// Let modules use hook_webform_validator_alter($validators) to change validator settings.
|
|
814 | 949 |
drupal_alter('webform_validator', $validators); |
950 |
|
|
951 |
// Remove entries for which only partial information exists. |
|
952 |
foreach ($validators as $validator_key => $validator_info) { |
|
953 |
if (!isset($validator_info['name']) || !isset($validator_info['component_types'])) { |
|
954 |
unset($validators[$validator_key]); |
|
955 |
} |
|
956 |
} |
|
957 |
|
|
815 | 958 |
return $validators; |
816 | 959 |
} |
817 | 960 |
|
818 | 961 |
/** |
819 |
* @todo Please document this function. |
|
820 |
* @see http://drupal.org/node/1354 |
|
962 |
* Return an array of the names of the validators. |
|
963 |
* |
|
964 |
* @return array |
|
965 |
* Array of with keys being validator IDs and values validator names. |
|
821 | 966 |
*/ |
822 | 967 |
function webform_validation_get_validators_selection() { |
823 | 968 |
$selection = array(); |
... | ... | |
838 | 983 |
$validators = webform_validation_get_validators(); |
839 | 984 |
if ($info = $validators[$validator]) { |
840 | 985 |
$allowed_types = $info['component_types']; |
841 |
if (_webform_validation_all_allowed($allowed_types)) {
|
|
986 |
if (in_array('all', $allowed_types, TRUE)) {
|
|
842 | 987 |
return array_keys(webform_components()); |
843 | 988 |
} |
844 | 989 |
return $info['component_types']; |
... | ... | |
846 | 991 |
} |
847 | 992 |
|
848 | 993 |
/** |
849 |
* Helper function to check whether all components are allowed to be used for a certain validator |
|
850 |
*/ |
|
851 |
function _webform_validation_all_allowed($allowed) { |
|
852 |
if ($allowed) { |
|
853 |
foreach ($allowed as $type) { |
|
854 |
if ($type == 'all') { |
|
855 |
return TRUE; |
|
856 |
} |
|
857 |
} |
|
858 |
} |
|
859 |
return FALSE; |
|
860 |
} |
|
861 |
|
|
862 |
/** |
|
863 |
* @todo Please document this function. |
|
864 |
* @see http://drupal.org/node/1354 |
|
994 |
* Return information about a specified validator. |
|
995 |
* |
|
996 |
* @param $validator_key string |
|
997 |
* The key of the validator to return information about. |
|
998 |
* |
|
999 |
* @return array |
|
1000 |
* Array of information about the validator. |
|
865 | 1001 |
*/ |
866 | 1002 |
function webform_validation_get_validator_info($validator_key) { |
867 | 1003 |
$validators = webform_validation_get_validators(); |
Formats disponibles : Unified diff
Weekly update of contrib modules