webform_validation.validators.inc
38.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
<?php
/**
* @file
* Provides validation functionality and hooks
*/
/**
* Implements hook_webform_validation_validators().
*
* This function returns an array of validators, in the validator key => options array form.
* Possible options:
* - name (required): name of the validator
* - component types (required): defines which component types can be validated by this validator. Specify 'all' to allow all types
* - negatable (optional): define whether the rule can be negated, meaning it will validate the inverse of the rule.
* - custom_error (optional): define whether a user can specify a custom error message upon creating the validation rule.
* - custom_data (optional): define whether custom data can be added to the validation rule
* - min_components (optional): define the minimum number of components to be selected for creating a validation rule
* - max_components (optional): define the maximum number of components to be selected for creating a validation rule
* - description (optional): provide a descriptive explanation about the validator
*/
function webform_validation_webform_validation_validators() {
$validators = array(
'numeric' => array(
'name' => t('Numeric values'),
'component_types' => array(
'hidden',
'number',
'textfield',
),
'custom_data' => array(
'label' => t('Specify numeric validation range'),
'description' => t('Optionally specify the minimum-maximum range to validate the user-entered numeric value against.') . ' ' . t('Usage') . ':'
. theme('item_list', array('items' => array(t('empty: no value validation'), t('"100": greater than or equal to 100'), t('"|100": less than or equal to 100 (including negative numbers)'), t('"0|100": greater than or equal to 0 & less than or equal to 100'), t('"10|100": greater than or equal to 10 & less than or equal to 100'), t('"-100|-10": greater than or equal to -100 & less than or equal to -10')))),
'required' => FALSE,
),
'description' => t('Verifies that user-entered values are numeric, with the option to specify min and / or max values.'),
),
'min_length' => array(
'name' => t('Minimum length'),
'component_types' => array(
'email',
'hidden',
'number',
'textarea',
'textfield',
),
'custom_data' => array(
'label' => t('Minimum number of characters'),
'description' => t('Specify the minimum number of characters that have to be entered to pass validation.'),
),
'description' => t('Verifies that a user-entered value contains at least the specified number of characters.'),
),
'max_length' => array(
'name' => t('Maximum length'),
'component_types' => array(
'email',
'hidden',
'number',
'textarea',
'textfield',
),
'custom_data' => array(
'label' => t('Maximum number of characters'),
'description' => t('Specify the maximum number of characters that can be entered to pass validation.'),
),
'description' => t('Verifies that a user-entered value contains at most the specified number of characters.'),
),
'min_words' => array(
'name' => t('Minimum number of words'),
'component_types' => array(
'hidden',
'html_textarea',
'textarea',
'textfield',
),
'custom_data' => array(
'label' => t('Minimum number of words'),
'description' => t('Specify the minimum number of words that have to be entered to pass validation. Words are defined as strings of letters separated by spaces.')
),
'description' => t('Verifies that a user-entered value contains at least the specified number of words.'),
),
'max_words' => array(
'name' => t('Maximum number of words'),
'component_types' => array(
'hidden',
'html_textarea',
'textarea',
'textfield',
),
'custom_data' => array(
'label' => t('Maximum number of words'),
'description' => t('Specify the maximum number of words that have to be entered to pass validation. Words are defined as strings of letters separated by spaces.')
),
'description' => t('Verifies that a user-entered value contains at most the specified number of words.'),
),
// Only available in Webform 4; removed below if not.
'sum' => array(
'name' => t('Adds up to'),
'component_types' => array(
'number',
),
'custom_data' => array(
'label' => t('Number the fields add up to'),
'description' => t('Specify the number and the type of comparison. For example:') . theme('item_list', array('items' => array(
t('Enter "=3" if the components must add up to exactly 3.'),
t('Enter ">10" if the components must add up to greater than 10.'),
t('Enter ">=10" if the components must add up to greater than or equal to 10.'),
t('Enter "<20" if the components must add up to less than 20.'),
t('Enter "<=20" if the components must add up to less than or equal to 20.'),
))),
),
'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.'),
),
'equal' => array(
'name' => t('Equal values'),
'component_types' => array(
'date',
'email',
'hidden',
'number',
'select',
'textarea',
'textfield',
'time',
),
'min_components' => 2,
'description' => t('Verifies that all specified components contain equal values. If all components are of type email, they will get case-insensitive comparison.'),
),
'comparison' => array(
'name' => t('Compare two values'),
'component_types' => array(
'date',
'email',
'hidden',
'number',
'select',
'textarea',
'textfield',
'time',
),
'custom_error' => TRUE,
'custom_data' => array(
'label' => t('Comparison operator'),
'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.'),
),
'min_components' => 2,
'max_components' => 2,
'description' => t('Compare two values for greater than (>), less than (<), greater than or equal to (>=), or less than or equal to (<=).'),
),
'unique' => array(
'name' => t('Unique values'),
'component_types' => array(
'date',
'email',
'hidden',
'number',
'select',
'textarea',
'textfield',
'time',
),
'min_components' => 2,
'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.'),
),
'specific_value' => array(
'name' => t('Specific value(s)'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'number',
'select',
'textarea',
'textfield',
),
'custom_error' => TRUE,
'custom_data' => array(
'label' => t('(Key) value'),
'description' => t('Specify the specific value(s) you want the component to contain. Separate multiple options by a comma. For components that have keys, use the key value instead.'),
),
'max_components' => 1,
'description' => t('Verifies that the value of the specified component is from a list of allowed values.'),
),
'default_value' => array(
'name' => t('Default value'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'number',
'select',
'textarea',
'textfield',
),
'custom_error' => TRUE,
'description' => t('Verifies that the user-entered value is the default value for that component. Negate if you want not the default value.'),
),
'someofseveral' => array(
'name' => t('Some of several'),
'component_types' => array(
'date',
'email',
'file',
'number',
'select',
'textarea',
'textfield',
'time'
),
'custom_data' => array(
'label' => t('Number to be completed'),
'description' => t('Specify the number that must be completed and the type of comparison. For example:') . theme('item_list', array('items' => array(
t('Enter ">=1" if the user must complete <b>at least</b> 1 of the selected components.'),
t('Enter "=3" if the user must complete <b>exactly</b> 3 of the selected components.'),
t('Enter "<=2" if the user must complete <b>at most</b> 2 of the selected components.'),
))),
),
'min_components' => 2,
'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.'),
),
'select_min' => array(
'name' => t('Minimum number of selections required'),
'component_types' => array(
'select',
),
'custom_data' => array(
'label' => t('Minimum number of selections'),
'description' => t('Specify the minimum number of options a user should select.'),
),
'description' => t('Forces the user to select at least a defined number of options from the specified webform components.'),
),
'select_max' => array(
'name' => t('Maximum number of selections allowed'),
'component_types' => array(
'select',
),
'custom_data' => array(
'label' => t('Maximum number of selections'),
'description' => t('Specify the maximum number of options a user can select.'),
),
'description' => t('Forces the user to select at most a defined number of options from the specified webform components.'),
),
'select_exact' => array(
'name' => t('Exact number of selections required'),
'negatable' => TRUE,
'component_types' => array(
'select',
),
'custom_data' => array(
'label' => t('Number of selections'),
'description' => t('Specify how many options a user can select.'),
),
'description' => t('Forces the user to select exactly the defined number of options from the specified webform components.'),
),
'plain_text' => array(
'name' => t('Plain text (disallow tags)'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'textarea',
'textfield',
),
'description' => t('Verifies that user-entered data doesn\'t contain HTML tags.'),
),
'starts_with' => array(
'name' => t('Starts with'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'number',
'textarea',
'textfield',
),
'custom_data' => array(
'label' => t('Starts with'),
'description' => t('Enter the text that this field must start with.'),
),
'description' => t('Verifies that user-entered data starts with a given string.'),
),
'ends_with' => array(
'name' => t('Ends with'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'number',
'textarea',
'textfield',
),
'custom_data' => array(
'label' => t('Ends with'),
'description' => t('Enter the text that this field must end with.'),
),
'description' => t('Verifies that user-entered data ends with a given string.'),
),
'pattern' => array(
'name' => t('Pattern'),
'component_types' => array(
'hidden',
'textarea',
'textfield',
),
'custom_error' => TRUE,
'custom_data' => array(
'label' => t('Pattern'),
'description' => t('Specify a pattern where:')
. theme('item_list', array('items' => array(t('@ = any letter A-Z'), t('# = any numeral 0-9'), t('| = separates two or more acceptable patterns'), t('Any other character must appear in its exact position'))))
. t('Examples')
. theme('item_list', array('items' => array(t('North American phone number: (###) ###-####'), t('D-2500 series model numbers: D-25##'), t('UK Postal Code: @# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA')))),
),
'description' => t('Verifies that a user-entered value follows to a specified pattern.'),
),
'regex' => array(
'name' => t('Regular expression, case-sensitive'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'number',
'textarea',
'textfield',
),
'custom_error' => TRUE,
'custom_data' => array(
'label' => t('Regex code'),
'description' => t('Specify regex code to validate the user input against.'),
),
'description' => t('Validates user-entered text against a specified case-sensitive regular expression. Note: don\'t include delimiters such as /.'),
),
'regexi' => array(
'name' => t('Regular expression, case-insensitive'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'number',
'textarea',
'textfield',
),
'custom_error' => TRUE,
'custom_data' => array(
'label' => t('Regex code'),
'description' => t('Specify regex code to validate the user input against.'),
),
'description' => t('Validates user-entered text against a specified case-insensitive regular expression. Note: don\'t include delimiters such as /.'),
),
'must_be_empty' => array(
'name' => t('Must be empty'),
'component_types' => array(
'hidden',
'number',
'textarea',
'textfield',
),
'description' => t('Verifies that a specified textfield remains empty. Recommended use case: used as an anti-spam measure by hiding the element with CSS.'),
),
'blacklist' => array(
'name' => t('Words blacklist'),
'negatable' => TRUE,
'component_types' => array(
'email',
'hidden',
'textarea',
'textfield',
),
'custom_error' => TRUE,
'custom_data' => array(
'label' => t('Blacklisted words'),
'description' => t('Specify illegal words, seperated by commas.'),
),
'description' => t('Validates that user-entered data doesn\'t contain any of the specified illegal words.'),
),
'username' => array(
'name' => t('Must match a username'),
'negatable' => TRUE,
'component_types' => array(
'hidden',
'textfield',
),
'description' => t('Validates that user-entered data matches a username.'),
),
'valid_url' => array(
'name' => t('Valid URL'),
'negatable' => TRUE,
'component_types' => array(
'hidden',
'textfield',
),
'description' => t('Validates that the user-entered data is a valid URL.'),
),
);
// Only available in Webform 4.
module_load_include('inc', 'webform', 'components/number');
if (!function_exists('webform_compare_floats')) {
unset($validators['sum']);
}
if (module_exists('email_verify')) {
$validators['email_verify'] = array(
'name' => t('Email Verify'),
'component_types' => array(
'email',
),
'description' => t('Verifies that an email address actually exists using the <a href="https://drupal.org/project/email_verify">Email Verification module</a>.'),
);
}
return $validators;
}
/**
* Implements hook_webform_validation_validate().
*/
function webform_validation_webform_validation_validate($validator_name, $items, $components, $rule) {
if (!$items) {
return NULL;
}
// For certain validators, if all components to be compared are email
// components, make the values lower-case to avoid case-sensitive comparison.
if (in_array($validator_name, array('equal', 'comparison', 'unique'))) {
$all_email = TRUE;
foreach ($rule['components'] as $component) {
if ($component['type'] !== 'email') {
$all_email = FALSE;
break;
}
}
if ($all_email) {
$items = array_map('strtolower', $items);
}
}
// Some components, such as multiple select, send their values as arrays, others don't.
// Convert all to arrays and write the rules to handle them that way. Remove empty values.
foreach ($items as $key => $val) {
$new_values = array();
foreach ((array) $val as $val_key => $val_val) {
if ((string) $val_val !== '') {
$new_values[$val_key] = $val_val;
}
}
$items[$key] = $new_values;
}
// Ensure later calls to key() get the first item.
reset($items);
$errors = array();
switch ($validator_name) {
case 'numeric':
$num_range = _webform_numeric_check_data($rule['data']);
foreach ($items as $key => $val) {
foreach ($val as $subval) {
// first check if the value is numeric
if (is_numeric($subval)) {
$subval = (float) $subval;
}
else {
$errors[$key] = t('%item is not numeric.', array('%item' => $components[$key]['name']));
continue;
}
// now validate the entered numeric value against the validator range settings, if appropriate
// a. validate min & max
if (isset($num_range['min']) && isset($num_range['max'])) {
// validate the min - max range
if ($subval < $num_range['min'] || $subval > $num_range['max']) {
$errors[$key] = t('%item is not within the allowed range %range.', array('%item' => $components[$key]['name'], '%range' => str_replace('|' , ' - ', $rule['data'])));
}
}
else {
// b. validate min
if (isset($num_range['min'])) {
if ($subval < $num_range['min']) {
$errors[$key] = t('%item should be greater than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['min']));
}
}
// c. validate max
if (isset($num_range['max'])) {
if ($subval > $num_range['max']) {
$errors[$key] = t('%item should be less than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['max']));
}
}
}
}
}
return $errors;
case 'min_length':
$min_length = $rule['data'];
foreach ($items as $key => $val) {
foreach ($val as $subval) {
if (drupal_strlen($subval) < $min_length) {
$errors[$key] = t('%item should be at least %num characters long.', array('%item' => $components[$key]['name'], '%num' => $min_length));
}
}
}
return $errors;
case 'max_length':
$max_length = $rule['data'];
foreach ($items as $key => $val) {
foreach ($val as $subval) {
if (drupal_strlen($subval) > $max_length) {
$errors[$key] = t('%item should be at most %num characters long.', array('%item' => $components[$key]['name'], '%num' => $max_length));
}
}
}
return $errors;
case 'min_words':
$min_words = $rule['data'];
foreach ($items as $key => $val) {
foreach ($val as $subval) {
if (_webform_validation_count_words($subval) < $min_words) {
$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']));
$errors[$key] = $error;
}
}
}
return $errors;
case 'max_words':
$max_words = $rule['data'];
foreach ($items as $key => $val) {
foreach ($val as $subval) {
if (_webform_validation_count_words($subval) > $max_words) {
$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']));
$errors[$key] = $error;
}
}
}
return $errors;
case 'sum':
// Get the numbers to sum.
$sum = array();
foreach ($items as $item) {
if (isset($item[0])) {
$sum[] = $item[0];
}
}
// If none of the components are completed, don't run this validator.
if (!count($sum)) {
return array();
}
$sum = array_sum($sum);
// Number being compared with.
$compare_number = (float) preg_replace('/^[^0-9]+/', '', $rule['data']);
// Parse the comparision operator and do comparison.
module_load_include('inc', 'webform', 'components/number');
$error = FALSE;
if (substr($rule['data'], 0, 2) === '<=') {
if (!(webform_compare_floats($sum, $compare_number) <= 0)) {
$error = t('less than or equal to');
}
}
elseif (substr($rule['data'], 0, 1) === '<') {
if (!(webform_compare_floats($sum, $compare_number) < 0)) {
$error = t('less than');
}
}
elseif (substr($rule['data'], 0, 2) === '>=') {
if (!(webform_compare_floats($sum, $compare_number) >= 0)) {
$error = t('greater than or equal to');
}
}
elseif (substr($rule['data'], 0, 1) === '>') {
if (!(webform_compare_floats($sum, $compare_number) > 0)) {
$error = t('greater than');
}
}
else {
if (!(webform_compare_floats($sum, $compare_number) === 0)) {
$error = t('exactly');
}
}
// Set error if needed.
if ($error) {
$keys = array_keys($items);
$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)));
}
return $errors;
case 'equal':
$first_entry_key = key($items);
$first_entry = array_shift($items);
foreach ($items as $key => $val) {
if ($val !== $first_entry) {
$errors[$key] = t('%item_checked does not match %item_first.', array('%item_checked' => $components[$key]['name'], '%item_first' => $components[$first_entry_key]['name']));
}
}
return $errors;
case 'comparison':
foreach (array(1, 2) as $count) {
$entry[$count]['key'] = key($items);
$value = array_shift($items);
if ($components[$entry[$count]['key']]['type'] === 'date') {
if (checkdate((int) $value['month'], (int) $value['day'], (int) $value['year'])) {
$entry[$count]['value'] = date('Y-m-d', mktime(0, 0, 0, (int) $value['month'], (int) $value['day'], (int) $value['year']));
}
else {
$entry[$count]['value'] = NULL;
}
}
elseif ($components[$entry[$count]['key']]['type'] === 'time') {
$time = $value['hour'] . ':' . $value['minute'];
if (!empty($value['ampm'])) {
$time .= ' ' . $value['ampm'];
}
$time = strtotime($time);
if ($time) {
$entry[$count]['value'] = date('H:i', $time);
}
else {
$entry[$count]['value'] = NULL;
}
}
else {
$entry[$count]['value'] = _webform_validation_flatten_array($value);
}
}
// Don't validate if either are empty.
if ((string) $entry[1]['value'] === '' || (string) $entry[2]['value'] === '') {
return array();
}
$error = FALSE;
switch ($rule['data']) {
case '>':
if (!($entry[1]['value'] > $entry[2]['value'])) {
$error = TRUE;
}
break;
case '>=':
if (!($entry[1]['value'] >= $entry[2]['value'])) {
$error = TRUE;
}
break;
case '<':
if (!($entry[1]['value'] < $entry[2]['value'])) {
$error = TRUE;
}
break;
case '<=':
if (!($entry[1]['value'] <= $entry[2]['value'])) {
$error = TRUE;
}
break;
}
if ($error) {
$errors[$entry[2]['key']] = _webform_validation_i18n_error_message($rule);
}
return $errors;
case 'unique':
foreach ($items as $key => $val) {
$items[$key] = _webform_validation_flatten_array($val);
}
// now we count how many times each value appears, and find out which values appear more than once
$items_count = array_count_values(array_map('drupal_strtolower', array_map('trim', $items)));
$doubles = array_filter($items_count, create_function('$x', 'return $x > 1;'));
foreach ($items as $key => $val) {
if (in_array(drupal_strtolower($val), array_keys($doubles))) {
$errors[$key] = t('The value of %item is not unique.', array('%item' => $components[$key]['name']));
}
}
return $errors;
case 'specific_value':
$specific_values = explode(',', $rule['data']);
$specific_values = array_map('trim', $specific_values);
foreach ($items as $key => $val) {
// Selects: Fail if at least one checked and at least one not in the allowed values.
$val = array_filter($val);
$test = count($val) && count(array_diff($val, $specific_values));
_webform_validation_test($errors, $key, $rule, $test);
}
return $errors;
case 'default_value':
foreach ($items as $key => $val) {
$val = _webform_validation_flatten_array($val);
$test = $val != $components[$key]['value'];
_webform_validation_test($errors, $key, $rule, $test);
}
return $errors;
case 'someofseveral':
// Determine the number of components completed.
foreach ($items as $key => $val) {
$items[$key] = _webform_validation_flatten_array($val);
$items[$key] = (bool) strlen($items[$key]);
}
$number_completed = count(array_filter($items));
// Number being compared with.
$compare_number = (int) preg_replace('/[^0-9]+/', '', $rule['data']);
if ($compare_number < 1) {
$compare_number = 1;
}
elseif ($compare_number > count($rule['components'])) {
$compare_number = count($rule['components']);
}
// Parse the comparision operator and do comparison.
$error = FALSE;
if (substr($rule['data'], 0, 1) === '=') {
if (!($number_completed === $compare_number)) {
$error = t('exactly');
}
}
elseif (substr($rule['data'], 0, 2) === '<=') {
if (!($number_completed <= $compare_number)) {
$error = t('at most');
}
}
else {
if (!($number_completed >= $compare_number)) {
$error = t('at least');
}
}
// Set error if needed.
if ($error) {
$keys = array_keys($items);
$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)));
}
return $errors;
case 'select_min':
$min_selections = $rule['data'];
foreach ($items as $key => $val) {
if (count(array_filter($val, '_webform_validation_check_false')) < $min_selections) {
$errors[$key] = t('Please select at least %num options for %item.', array('%num' => $min_selections, '%item' => $components[$key]['name']));
}
}
return $errors;
case 'select_max':
$max_selections = $rule['data'];
foreach ($items as $key => $val) {
if (count(array_filter($val, '_webform_validation_check_false')) > $max_selections) {
$errors[$key] = t('Please select maximum %num options for %item.', array('%num' => $max_selections, '%item' => $components[$key]['name']));
}
}
return $errors;
case 'select_exact':
$allowed_selections = $rule['data'];
foreach ($items as $key => $val) {
$error_strings = array(
'regular' => 'Please select %num options for %item.',
'negated' => 'Please do not select %num options for %item.',
);
$error_vars = array('%num' => $allowed_selections, '%item' => $components[$key]['name']);
$test = count(array_filter($val, '_webform_validation_check_false')) != $allowed_selections;
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
}
return $errors;
case 'plain_text':
foreach ($items as $key => $val) {
$error_strings = array(
'regular' => '%item only allows the use of plain text.',
'negated' => '%item must contain HTML tags.',
);
$error_vars = array('%item' => $components[$key]['name']);
foreach ($val as $subval) {
$test = strcmp($subval, strip_tags($subval));
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
}
}
return $errors;
case 'starts_with':
case 'ends_with':
$pattern = preg_quote($rule['data'], '/');
if ($validator_name === 'starts_with') {
$pattern = '^' . $pattern;
$verb = t('start');
}
else {
$pattern .= '$';
$verb = t('end');
}
$pattern = '/' . $pattern . '/';
foreach ($items as $key => $val) {
$error_strings = array(
'regular' => '%item must %verb with "%string".',
'negated' => '%item must not %verb with "%string".',
);
$error_vars = array('%item' => $components[$key]['name'], '%verb' => $verb, '%string' => $rule['data']);
foreach ($val as $subval) {
$test = !preg_match($pattern, $subval);
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
}
}
return $errors;
case 'pattern':
$pattern = preg_quote($rule['data'], '/');
$pattern = str_replace('@', '[a-zA-Z]', $pattern);
$pattern = str_replace('#', '[0-9]', $pattern);
$pattern = '(' . $pattern . ')';
// Un-escape "|" operator.
$pattern = preg_replace('/(\\s*)(\\\\)(\\|)(\\s*)/', ')|(', $pattern);
foreach ($items as $key => $val) {
foreach ($val as $subval) {
$test = !preg_match('/^(' . $pattern . ')$/', $subval);
_webform_validation_test($errors, $key, $rule, $test);
}
}
return $errors;
case 'regex':
case 'regexi':
mb_regex_encoding('UTF-8');
$regex = $rule['data'];
foreach ($items as $key => $val) {
foreach ($val as $subval) {
if ($validator_name === 'regexi') {
$match = (bool) mb_eregi($regex, $subval);
}
else {
$match = (bool) mb_ereg($regex, $subval);
}
$test = !$match;
_webform_validation_test($errors, $key, $rule, $test);
}
}
return $errors;
case 'must_be_empty':
foreach ($items as $key => $val) {
if (count($val) !== 0) {
$errors[$key] = t('%item does not contain the correct data.', array('%item' => $components[$key]['name']));
}
}
return $errors;
case 'blacklist':
$blacklist = preg_quote($rule['data'], '/');
$blacklist = explode(',', $blacklist);
$blacklist = array_map('trim', $blacklist);
$blacklist = '/\b(' . implode('|', $blacklist) . ')\b/i';
foreach ($items as $key => $val) {
foreach ($val as $subval) {
$test = preg_match($blacklist, $subval);
_webform_validation_test($errors, $key, $rule, $test);
}
}
return $errors;
case 'username':
foreach ($items as $key => $val) {
$error_strings = array(
'regular' => 'The %item field does not match an active username.',
'negated' => 'The %item field matches an active username.',
);
$error_vars = array('%item' => $components[$key]['name']);
foreach ($val as $subval) {
$test = !db_query_range('SELECT 1 FROM {users} WHERE name = :username AND status = 1', 0, 1, array(':username' => $subval))->fetchField();
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
}
}
return $errors;
case 'valid_url':
foreach ($items as $key => $val) {
$error_strings = array(
'regular' => '%item does not appear to be a valid URL.',
'negated' => '%item must not be a valid URL.',
);
$error_vars = array('%item' => $components[$key]['name']);
foreach ($val as $subval) {
$test = !valid_url($subval, TRUE);
_webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
}
}
return $errors;
case 'email_verify':
if (module_exists('email_verify')) {
foreach ($items as $key => $val) {
foreach ($val as $subval) {
$error = email_verify_check($subval);
if ($error) {
$errors[$key] = $error;
}
}
}
}
return $errors;
}
}
/**
* Return an array of the names of the components in a validation rule.
*
* @param $rule array
* Array of information about a validation rule.
*
* @return array
* Array of the filtered names of the components in $rule.
*/
function _webform_validation_get_names_of_rule_components(array $rule) {
$names = array();
foreach ($rule['components'] as $component) {
$names[] = _webform_filter_xss($component['name']);
}
return $names;
}
/**
* Helper function to negate validation rules as needed and create the correct error message.
*/
function _webform_validation_test(&$errors, $key, $rule, $test, array $error_strings = NULL, array $error_vars = NULL) {
$rule['negate'] = !empty($rule['negate']);
if ($rule['negate']) {
$test = !$test;
}
if ($test) {
if ($error_strings) {
$error = t($error_strings[$rule['negate'] ? 'negated' : 'regular'], $error_vars);
}
else {
$error = _webform_validation_i18n_error_message($rule);
}
$errors[$key] = $error;
}
}
/**
* Count the number of words in a value.
*
* Strip HTML first.
*/
function _webform_validation_count_words($val) {
$val = _webform_validation_flatten_array($val);
$val = strip_tags($val);
// Replace entities representing spaces with actual spaces.
$val = str_replace(' ', ' ', $val);
$val = str_replace(' ', ' ', $val);
return str_word_count($val);
}
/**
* Helper function to deal with submitted values that are arrays (for example, multiple select component).
* We flatten the array as a comma-separated list to do the comparison.
*/
function _webform_validation_flatten_array($val) {
if (is_array($val)) {
$arr = array_filter($val, '_webform_validation_check_false');
return implode(',', $arr);
}
return $val;
}
/**
* Get a list of validator definitions.
*
* @return
* Array of information about validators.
*/
function webform_validation_get_validators() {
$validators = module_invoke_all('webform_validation_validators');
// Let modules use hook_webform_validator_alter($validators) to change validator settings.
drupal_alter('webform_validator', $validators);
// Remove entries for which only partial information exists.
foreach ($validators as $validator_key => $validator_info) {
if (!isset($validator_info['name']) || !isset($validator_info['component_types'])) {
unset($validators[$validator_key]);
}
}
return $validators;
}
/**
* Return an array of the names of the validators.
*
* @return array
* Array of with keys being validator IDs and values validator names.
*/
function webform_validation_get_validators_selection() {
$selection = array();
$validators = webform_validation_get_validators();
if ($validators) {
foreach ($validators as $validator_key => $validator_info) {
$selection[$validator_key] = $validator_info['name'];
}
}
return $selection;
}
/**
* Get a list of valid component types per validator, as defined via hook_webform_validation_validators().
* If 'all' is specified, all available component types will be returned.
*/
function webform_validation_valid_component_types($validator) {
$validators = webform_validation_get_validators();
if ($info = $validators[$validator]) {
$allowed_types = $info['component_types'];
if (in_array('all', $allowed_types, TRUE)) {
return array_keys(webform_components());
}
return $info['component_types'];
}
}
/**
* Return information about a specified validator.
*
* @param $validator_key string
* The key of the validator to return information about.
*
* @return array
* Array of information about the validator.
*/
function webform_validation_get_validator_info($validator_key) {
$validators = webform_validation_get_validators();
return $validators[$validator_key];
}
/**
* Handle translatable error messages, if available
*/
function _webform_validation_i18n_error_message($rule) {
$rule['error_message'] = filter_xss($rule['error_message']);
if (module_exists('i18n')) {
return i18n_string('webform_validation:error_message:' . $rule['ruleid'] . ':message', $rule['error_message']);
}
return $rule['error_message'];
}
/**
* Helper function used by array_filter to determine if a value was selected or not
*/
function _webform_validation_check_false($var) {
return $var !== FALSE && $var !== 0;
}
/**
* Process the numeric value validation range that was provided in the numeric validator options
*/
function _webform_numeric_check_data($data) {
$range = array('min' => NULL, 'max' => NULL);
// if no value was specified, don't validate
if ($data == '') {
return $range;
}
// If only one numeric value was specified, this is the min value
if (is_numeric($data)) {
$range['min'] = (float) $data;
}
if (strpos($data, '|') !== FALSE) {
list($min, $max) = explode('|', $data);
if ($min != '' && is_numeric($min)) {
$range['min'] = (float) $min;
}
if ($max != '' && is_numeric($max)) {
$range['max'] = (float) $max;
}
}
return $range;
}