-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_validator.cfc
1513 lines (1153 loc) · 62.3 KB
/
form_validator.cfc
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
<!---
@author Drew Dulgar / Tristan Lee
@date 10/12/09
@purpose Class used for validating form fields
@changlog 3/28/10 Converted from ColdBox plugin to basic CFC
5/04/10 Added function to set look-up values from a query
ROUTINE EXAMPLES
- (!) indicates the routine naturally supports negation
----------------
> required :: requires the field
> required[fieldName] :: requires the field if 'fieldName' has any value
> required[fieldName=value] :: requires the field if 'fieldName' has a value of 'value'
> (!)matches[fieldName] :: field must match 'fieldName'
> alpha :: field must only contain alpha characters
> alpha_dash :: field must only contain alpha and hyphen characters
> alpha_numeric :: field must only contain alphanumeric characters
> between[value1|value2] :: field must be between the values specified in the argument list (delimited by a pipe)
> contains[value] :: field must contain 'value'
> does_not_contain[value] :: field must not contain 'value'
> equals[value] :: field must equal 'value'
> exact_length[len] :: field must have an exact length of 'len'
> greater_than[fieldName] :: field must be greater than the value of the field 'fieldName'
> greater_than[value] :: field must be greater than 'value'
> greater_than_datetime[fieldName] :: field must have a date greather than the value of the field 'fieldName'
> greater_than_datetime[value] :: field must have a date greater than 'value'
> greater_than_or_equal_to[fieldName] :: field must be greater than or equal to the value of the field 'fieldName'
> greater_than_or_equal_to[value] :: field must be greater than or equal to 'value'
> greater_than_or_equal_to_datetime[fieldName] :: field must have a date greater than or equal to the value of the field 'fieldName'
> greater_than_or_equal_to_datetime[value] :: field must have a date greater than or equal to 'value'
> integer :: field must be an interger
> is_empty[fieldName|fieldName1|fieldNameN...] :: at least one of the fields specified in the argument list (delimited by a pipe) must have a value
> is_natural :: field must be a natural number
> is_natural_no_zero :: field must be a natural number, excluding 0
> less_than[fieldName] :: field must be less than the value of the field 'fieldName'
> less_than[value] :: field must be less than 'value'
> less_than_datetime[fieldName] :: field must have a date less than the value of the field 'fieldName'
> less_than_datetime[value] :: field must have a date less than 'value'
> less_than_or_equal_to[fieldName] :: field must be less than or equal to the value of the field 'fieldName'
> less_than_or_equal_to[value] :: field must be less than or equal to 'value'
> less_than_or_equal_to_datetime[fieldName] :: field must have a date less than or equal to the value of the field 'fieldName'
> less_than_or_equal_to_datetime[value] :: field must have a date less than or equal to 'value'
> lookup_value[lookup] :: field must be one of the values in the specified 'lookup'
> max_decimal[precision|scale] :: field must be a decimal no larger than the specified 'precision' and 'scale'
> max_length[len] :: field must have a maximum length of 'len'
> min_length[len] :: field must have a minimum length of 'len'
> numeric :: field must be a numeric value
> select_at_least[value] :: number of field values (delimited by a pipe) must be at least 'value'
> select_no_more_than[value] :: number of field values (delimited by a pipe) must be no more than 'value'
> valid_base64 :: field must be of valid base64 representation
> valid_creditcard :: field must be of valid creditcard format
> valud_currencyUS :: field must be of valid US currency format
> valid_date :: field must be of valid US date format
> valid_email :: field must be of valid e-mail address format
> valid_email_list :: list of field values must be of valid e-mail address format
> valid_ip :: field must be of valid IPv4 address format
> valid_pdf[filepath] :: 'filepath' must be a path to a PDF file readable by CFPDF
> valid_phoneUS :: field must be of valid US phone number format
> valid_postal :: field must be of valid postal code format for US and Canada
> valid_ssn :: field must be of valid SSN format
> valid_time :: field must be of valid time format
> valid_url :: field must be of valid URL format
> value_is_one_of[value|value1|valueN...] :: field must be one of the values specified in the argument list (delimited by a pipe)
--->
<cfcomponent name="FormValidator" output="false">
<!--- Private members --->
<cfset variables.lookups = {}>
<cfset variables.validation = {}>
<cfset variables.validation_order = []>
<cfset variables.errors = []>
<cfset variables.errorFields = {}>
<cfset variables.rc = {}>
<cfset variables.run = false>
<cfset variables.trigger = {}>
<cfset variables.interceptionMethods = {}>
<!--- Static list of CFCs to automatically import on instantiation --->
<cfset variables.importCFCs = [] />
<cfset variables.settings = {}>
<cfset variables.settings.returnSelectors = true>
<cfset variables.settings.persistDefaultValue = false>
<cfset variables.settings.errors = {}>
<cfset variables.settings.errors.unique = {}>
<cfset variables.settings.errors['required'] = 'The field [LABEL] is required.'>
<cfset variables.settings.errors['matches'] = 'The [LABEL] field does not match the [ARGUMENT] field.'>
<cfset variables.settings.errors['!matches'] = 'The [LABEL] field can not match the [ARGUMENT] field.'>
<cfset variables.settings.errors['min_length'] = 'The [LABEL] field must be at least [ARGUMENT] character(s).'>
<cfset variables.settings.errors['max_length'] = 'The [LABEL] field must be at most [ARGUMENT] character(s).'>
<cfset variables.settings.errors['exact_length'] = 'The [LABEL] field must be exactly [ARGUMENT] characters(s).'>
<cfset variables.settings.errors['greater_than'] = 'The [LABEL] field must be greater than [ARGUMENT].'>
<cfset variables.settings.errors['greater_than_datetime'] = 'The [LABEL] field must be after [ARGUMENT_VALUE].'>
<cfset variables.settings.errors['greater_than_or_equal_to'] = 'The [LABEL] field must be greater than or equal to [ARGUMENT].'>
<cfset variables.settings.errors['greater_than_or_equal_to_datetime'] = 'The [LABEL] field must be on or after [ARGUMENT_VALUE].'>
<cfset variables.settings.errors['less_than'] = 'The [LABEL] field must be less than [ARGUMENT].'>
<cfset variables.settings.errors['less_than_datetime'] = 'The [LABEL] field must be before [ARGUMENT_VALUE].'>
<cfset variables.settings.errors['less_than_or_equal_to'] = 'The [LABEL] field must be less than or equal to [ARGUMENT].'>
<cfset variables.settings.errors['less_than_or_equal_to_datetime'] = 'The [LABEL] field must be on or before [ARGUMENT_VALUE].'>
<cfset variables.settings.errors['select_at_least'] = 'Select at least [ARGUMENT] items for the [LABEL] field.'>
<cfset variables.settings.errors['select_no_more_than'] = 'Select no more than [ARGUMENT] items for the [LABEL] field.'>
<cfset variables.settings.errors['contains'] = 'The [LABEL] field must contain the value [ARGUMENT].'>
<cfset variables.settings.errors['does_not_contain'] = 'The [LABEL] field should not contain the [ARGUMENT] field.'>
<cfset variables.settings.errors['valid_email'] = 'The [LABEL] field is not a valid email.'>
<cfset variables.settings.errors['alpha'] = 'The [LABEL] field can only be alpha characters.'>
<cfset variables.settings.errors['alpha_numeric'] = 'The [LABEL] field can only be alpha numeric characters and numbers.'>
<cfset variables.settings.errors['alpha_dash'] = 'The [LABEL] field can only contain alpha numeric characters, underscores, and dashes.'>
<cfset variables.settings.errors['numeric'] = 'The [LABEL] field must be numeric.'>
<cfset variables.settings.errors['integer'] = 'The [LABEL] field must be an integer.'>
<cfset variables.settings.errors['is_natural'] = 'The [LABEL] field must be a natural number.'>
<cfset variables.settings.errors['is_natural_no_zero'] = 'The [LABEL] must be a natural number greater than 0.'>
<cfset variables.settings.errors['valid_base64'] = 'The [LABEL] field must be a valid base64 string.'>
<cfset variables.settings.errors['is_valid_value'] = 'The [LABEL] field does not match the [ARGUMENT] field.'>
<cfset variables.settings.errors['value_is_one_of'] = 'The [LABEL] field does contain one of the values in [ARGUMENT].'>
<cfset variables.settings.errors['lookup_value'] = 'The [LABEL] field does not have a valid value.'>
<cfset variables.settings.errors['valid_url'] = 'The [LABEL] field must be a valid url.'>
<cfset variables.settings.errors['valid_ip'] = 'The [LABEL] field must be a valid ip address.'>
<cfset variables.settings.errors['valid_ssn'] = 'The [LABEL] field is not a valid social security number.'>
<cfset variables.settings.errors['valid_postal'] = 'The [LABEL] field is not a valid postal address.'>
<cfset variables.settings.errors['valid_date'] = 'The [LABEL] field is not a valid date.'>
<cfset variables.settings.errors['valid_time'] = 'The [LABEL] field is not a valid time.'>
<cfset variables.settings.errors['valid_phoneUS'] = 'The [LABEL] field is not a valid phone number.'>
<cfset variables.settings.errors['valid_currencyUS'] = 'The [LABEL] is not a valid currency amount.'>
<cfset variables.settings.errors['valid_creditcard'] = 'The [LABEL] field is not a valid credit card number.'>
<cfset variables.settings.errors['valid_file'] = 'The [LABEL] field does not contain a valid file type.'>
<cfset variables.settings.errors['between'] = 'The [LABEL] field must be between [ARGUMENT] characters.'>
<cfset variables.mimeTypes = {}>
<cffunction name="init" access="public" returntype="FormValidator" output="false" hint="Pass in as many structures for values as you want. First passed in structure takes precedence over the next. First come First Serve.">
<cfargument name="collection" required="true" type="struct" >
<cfset var local = structNew()>
<cfset variables.rc = arguments.collection>
<cfset structDelete(arguments, 'collection')>
<cfif NOT structIsEmpty(arguments)>
<cfloop collection="#arguments#" item="local.key">
<cfset structAppend(variables.rc, arguments[local.key])>
</cfloop>
</cfif>
<!--- clean all the values as needed --->
<cfloop collection="#variables.rc#" item="local.key">
<cfif isSimpleValue(variables.rc[local.key])>
<cfset variables.rc[local.key] = trim(variables.rc[local.key])>
</cfif>
</cfloop>
<!--- Import CFC methods --->
<cfloop array="#variables.importCFCs#" index="local.cfcName">
<cfset importRoutines(local.cfcName) />
</cfloop>
<!--- Return instance --->
<cfreturn this>
</cffunction>
<cffunction name="getValue" access="public" returntype="string" output="false">
<cfargument name="item" type="string" required="true">
<cfargument name="default" type="string" required="true" default="">
<cfif structKeyExists(variables.rc, arguments.item)>
<cfreturn variables.rc[arguments.item]>
<cfelse>
<cfreturn arguments.default>
</cfif>
</cffunction>
<cffunction name="getMimeType" access="public" returntype="array" output="false">
<cfargument name="ext" type="string" required="true">
<cfif structKeyExists(variables.mimeTypes, ext)>
<cfreturn listToArray(variables.mimeTypes[ext])>
</cfif>
<cfreturn []>
</cffunction>
<cffunction name="setReturnSelectors" access="public" returntype="void" output="false">
<cfargument name="value" type="string" required="true" default="boolean">
<cfswitch expression="#arguments.value#">
<cfcase value="boolean">
<cfset variables.settings.returnSelectors = false>
</cfcase>
<cfcase value="text">
<cfset variables.settings.returnSelectors = true>
</cfcase>
<cfdefaultcase>
<cfset variables.settings.returnSelectors = false>
</cfdefaultcase>
</cfswitch>
</cffunction>
<cffunction name="setTrigger" access="public" returntype="void" output="false">
<cfargument name="name" type="string" required="true">
<cfargument name="value" type="string" required="false" default="">
<cfset variables.trigger.name = arguments.name>
<cfset variables.trigger.value = arguments.value>
</cffunction>
<cffunction name="setMethod" access="public" returntype="void" output="false">
<cfargument name="name" required="true" type="string">
<cfargument name="method" required="true" type="any">
<cfargument name="message" required="false" type="string" />
<cfset variables[arguments.name] = arguments.method>
<cfif structKeyExists(arguments, "message") && len(trim(arguments.message)) GT 0>
<cfset setErrorMessage(arguments.name, arguments.message) />
</cfif>
</cffunction>
<cffunction name="setPreValidation" access="public" returntype="void" output="false"
hint="Executes before all form fields get validated.">
<cfargument name="function" required="true" type="any">
<cfset variables.interceptionMethods['preInterception'] = arguments.function>
</cffunction>
<cffunction name="setPostValidation" access="public" returntype="void" output="false"
hint="Eexecutes after all form fields get validated.">
<cfargument name="function" required="true" type="any">
<cfset variables.interceptionMethods['postInterception'] = arguments.function>
</cffunction>
<cffunction name="setLookupValue" access="public" returntype="void" output="false">
<cfargument name="name" type="string" required="true">
<cfargument name="key" type="any" required="true">
<cfargument name="value" type="any" required="false" default="">
<cfset local = {}>
<cfset local.key = arguments.key>
<cfset local.value = arguments.value>
<cfif len(trim(local.value)) EQ 0>
<cfset local.value = local.key>
</cfif>
<cfif NOT structKeyExists(variables.lookups, arguments.name)>
<cfset variables.lookups[arguments.name] = []>
</cfif>
<cfset arrayAppend(variables.lookups[arguments.name], local)>
</cffunction>
<cffunction name="setLookupValuesFromQuery" access="public" returntype="void" output="false">
<cfargument name="query" type="query" required="true">
<cfargument name="name" type="string" required="true">
<cfargument name="key" type="string" required="true">
<cfargument name="value" type="any" required="false" default="">
<cfloop query="arguments.query">
<cfset setLookupValue(arguments.name, arguments.query[arguments.key], arguments.query[arguments.value])>
</cfloop>
</cffunction>
<cffunction name="getLookup" access="public" returntype="array" output="false">
<cfargument name="name" type="string" required="true">
<cfif structKeyExists(variables.lookups, arguments.name)>
<cfreturn variables.lookups[arguments.name]>
</cfif>
<cfreturn []>
</cffunction>
<cffunction name="setValidation" access="public" returntype="void" output="false">
<cfargument name="name" required="true" type="string">
<cfargument name="label" required="false" type="string">
<cfargument name="validation" required="false" type="string">
<cfif arguments.label EQ ''>
<cfset arguments.label = arguments.name>
</cfif>
<cfif structKeyExists(variables.validation, arguments.name)>
<cfset variables.validation[arguments.name].validators = variables.validation[arguments.name].validators & ',' & arguments.validation>
<cfelse>
<cfset arrayAppend(variables.validation_order, arguments.name)>
<cfset variables.validation[arguments.name] = {}>
<cfset variables.validation[arguments.name].label = arguments.label>
<cfset variables.validation[arguments.name].validators = arguments.validation>
</cfif>
</cffunction>
<cffunction name="getValidation" access="public" returntype="struct" output="false">
<cfreturn variables.validation />
</cffunction>
<cffunction name="getLabel" access="public" returntype="string" output="false">
<cfargument name="field" type="string" required="true" />
<cfif structKeyExists(variables.validation, arguments.field) AND structKeyExists(variables.validation[arguments.field], "label")>
<cfreturn variables.validation[arguments.field].label />
</cfif>
<cfreturn "" />
</cffunction>
<cffunction name="run" access="public" returntype="boolean" output="false">
<cfargument name="runWithoutTrigger" type="boolean" default="false" />
<cfset var local = {}>
<cfset variables.run = false>
<cfif arguments.runWithoutTrigger OR
(
structKeyExists(variables.trigger, 'name') AND
structKeyExists(variables.trigger, 'value') AND
(
structKeyExists(variables.rc, variables.trigger.name) AND
variables.trigger.value EQ ''
)
)
OR
(
variables.trigger.value NEQ ''
AND structKeyExists(variables.rc, variables.trigger.name)
AND variables.rc[variables.trigger.name] EQ variables.trigger.value
)>
<cfset variables.run = true>
<cfif structKeyExists(variables.interceptionMethods, 'preInterception')>
<cfset variables.interceptionMethods.preInterception() />
</cfif>
<cfloop from="1" to="#arrayLen(variables.validation_order)#" index="local.i">
<cfset validateField( variables.validation_order[local.i],
variables.validation[variables.validation_order[local.i]].validators,
variables.validation[variables.validation_order[local.i]].label)>
</cfloop>
<cfif structKeyExists(variables.interceptionMethods, 'postInterception')>
<cfset variables.interceptionMethods.postInterception() />
</cfif>
</cfif>
<cfreturn validated()>
</cffunction>
<cffunction name="getErrors" access="public" returntype="array" output="false">
<cfset var local = structNew() />
<cfreturn variables.errors>
</cffunction>
<cffunction name="totalErrors" access="public" returntype="numeric" output="false">
<cfset var local = structNew() />
<cfreturn arrayLen(getErrors()) />
</cffunction>
<cffunction name="displayErrors" access="public" returntype="string" output="false">
<cfargument name="prepend" default="<li>" required="no" type="string">
<cfargument name="append" default="</li>" required="no" type="string">
<cfset var local = {}>
<cfset local.error = ''>
<cfif NOT validated()>
<cfset local.errors = getErrors()>
<cfloop from="1" to="#ArrayLen(local.errors)#" index="local.i">
<cfset local.error = local.error & arguments.prepend & local.errors[local.i] & arguments.append>
</cfloop>
</cfif>
<cfreturn local.error>
</cffunction>
<cffunction name="validated" access="public" returntype="boolean" output="false">
<cfif variables.run EQ 0 OR ArrayLen(variables.errors) GT 0>
<cfreturn false>
</cfif>
<cfreturn true>
</cffunction>
<cffunction name="getDefaultValue" output="false" returntype="string" access="private">
<cfargument name="field" default="" required="true" type="string">
<cfargument name="default" default="" required="true" type="string">
<cfif variables.run EQ 0 OR variables.settings.persistDefaultValue EQ 1>
<cfreturn arguments.default>
<cfelseif structKeyExists(variables.rc, arguments.field)>
<cfreturn variables.rc[arguments.field]>
</cfif>
<cfreturn ''>
</cffunction>
<cffunction name="setValue" output="false" returntype="string" access="public">
<cfargument name="field" default="" required="true" type="string">
<cfargument name="default" default="" required="false" type="string">
<cfreturn getDefaultValue(arguments.field, arguments.default)>
</cffunction>
<cffunction name="setSelect" output="false" returntype="any" access="public">
<cfargument name="field" default="" required="true" type="string">
<cfargument name="value" default="" required="true" type="string">
<cfargument name="default" default="" required="false" type="string">
<cfset var local = {}>
<cfset local.value = getDefaultValue(arguments.field, arguments.default)>
<cfif listFindNoCase(local.value, arguments.value)>
<cfif variables.settings.returnSelectors EQ true>
<cfreturn ' selected="selected"'>
<cfelse>
<cfreturn true>
</cfif>
<cfelseif variables.settings.returnSelectors EQ false>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="setRadio" output="false" returntype="string" access="public">
<cfargument name="field" default="" required="true" type="string">
<cfargument name="value" default="" required="true" type="string">
<cfargument name="default" default="" required="false" type="string">
<cfset var local = {}>
<cfset local.value = getDefaultValue(arguments.field, arguments.default)>
<cfif listFindNoCase(local.value, arguments.value)>
<cfif variables.settings.returnSelectors EQ true>
<cfreturn ' checked="checked"'>
<cfelse>
<cfreturn true>
</cfif>
<cfelseif variables.settings.returnSelectors EQ false>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="setCheckbox" output="false" returntype="string" access="public">
<cfargument name="field" default="" required="true" type="string">
<cfargument name="value" default="" required="true" type="string">
<cfargument name="default" default="" required="false" type="string">
<cfset var local = {}>
<cfset local.value = getDefaultValue(arguments.field, arguments.default)>
<cfif listFindNoCase(local.value, arguments.value)>
<cfif variables.settings.returnSelectors EQ true>
<cfreturn ' checked="checked"'>
<cfelse>
<cfreturn true>
</cfif>
<cfelseif variables.settings.returnSelectors EQ false>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="validateField" output="false" returntype="boolean" access="private">
<cfargument name="field" default="" required="true" type="string">
<cfargument name="validations" default="" required="false" type="string">
<cfargument name="label" default="" required="false" type="string">
<cfset var local = {}>
<!--- pass if we have no validations for this field --->
<cfif trim(arguments.validations) EQ ''>
<cfreturn true>
</cfif>
<!--- get the value of this field --->
<cfset local.value = getValue(arguments.field) />
<!--- find either 'routine_name' OR 'routine_name[arguments]' --->
<cfset local.searchRegex = '(\!)?([^\[\],]+)(?:\[([^\]]*)\])?' />
<cfset local.pattern = createObject("java", "java.util.regex.Pattern").compile(local.searchRegex) />
<cfset local.matcher = local.pattern.matcher(arguments.validations) />
<cfset local.arValidation = [] />
<!--- iterate the matches, creating a structure of items for each validation specified --->
<cfloop condition="#local.matcher.find()#">
<cfset local.thisValidation = {negates = local.matcher.group(1), routine = local.matcher.group(2), arguments = local.matcher.group(3), validation = local.matcher.group()} />
<!--- --->
<cfif (structkeyExists(local.thisValidation, "negates") && local.thisValidation.negates EQ "!")>
<cfset local.thisValidation.negates = true />
<cfelse>
<cfset local.thisValidation.negates = false />
</cfif>
<!--- no arguments exist for this routine, so set it as an empty string --->
<cfif !structkeyExists(local.thisValidation, "arguments")>
<cfset local.thisValidation.arguments = "" />
</cfif>
<!--- the 'required' routine should always be first to execute to prepend it to the array --->
<cfif local.thisValidation.routine EQ "required">
<cfset arrayPrepend(local.arValidation, local.thisValidation) />
<cfelse>
<cfset arrayAppend(local.arValidation, local.thisValidation) />
</cfif>
</cfloop>
<!--- if no 'required' (without conditions) routine is specified,
and the field doesn't have a value, there's nothing to validation --->
<cfif arrayLen(local.arValidation) GT 0 &&
local.arValidation[1].routine NEQ "required" &&
len(local.arValidation[1].arguments) EQ 0 &&
local.value EQ "">
<cfreturn true />
</cfif>
<!--- Loop all of the validation routines and test the field for validity --->
<cfloop array="#local.arValidation#" index="local.validation">
<!--- execute our validation method and set our status based on it. --->
<cfif isDefined("#trim(local.validation.routine)#")>
<cfinvoke method = "#local.validation.routine#"
value = "#local.value#"
argument = "#local.validation.arguments#"
field = "#arguments.field#"
returnvariable = "local.test">
<cfif (
local.validation.routine EQ "required"
OR len(local.value) GT 0
OR NOT structKeyExists(variables.rc, arguments.field)
)
<!---AND (NOT local.test AND NOT local.validation.negates)--->
AND ((local.validation.negates && local.test) || (!local.validation.negates && !local.test))
>
<cfset setError(local.validation.routine, arguments.field, local.validation.arguments, local.value, local.validation.negates)>
<cfreturn false>
</cfif>
<cfelse>
<cfthrow type="custom" message="Validation Routine #local.validation.routine# Does Not Exist" extendedinfo="The #local.validation.routine# validation routine method does not exist. It was set on the field #arguments.field#. Please check to make sure the method exists and that its name is spelled correctly.">
</cfif>
</cfloop>
<cfreturn true>
</cffunction>
<!--- set a validation methods global error message (ie any field ) --->
<cffunction name="setErrorMessage" output="false" access="public" returntype="void">
<cfargument name="method" required="true" type="string">
<cfargument name="message" required="false" default="" type="string">
<cfset variables.settings.errors[arguments.method] = arguments.message>
</cffunction>
<!--- set a validation methods error message specific to a field --->
<cffunction name="setFieldErrorMessage" output="false" access="public" returntype="void">
<cfargument name="field" required="true" type="string">
<cfargument name="method" required="true" type="string">
<cfargument name="message" required="false" default="" type="string">
<cfset variables.settings.errors.unique[arguments.field][arguments.method] = arguments.message>
</cffunction>
<!--- set and add an error message based on the given field and the fields specific error message --->
<cffunction name="setError" output="false" access="public" returntype="void">
<cfargument name="method" required="true" type="string">
<cfargument name="field" required="true" type="string">
<cfargument name="argument" required="false" default="" type="string">
<cfargument name="value" required="false" type="string">
<cfargument name="negates" required="false" type="boolean" default="false">
<cfset var local = {}>
<cfif arguments.negates>
<cfset arguments.method = "!#arguments.method#" />
</cfif>
<cfset local.label = 'Label not set'>
<cfset local.message = getErrorMessage(arguments.field, arguments.method)>
<!--- in case the argument has multiple values, get just the first --->
<cfset arguments.argument = getToken(arguments.argument, 1, '|') />
<!--- get the label for this field --->
<cfif structKeyExists(variables.validation, arguments.field)>
<cfset local.label = variables.validation[arguments.field].label>
</cfif>
<cfset local.message = Replace(local.message, '[LABEL]', local.label, 'all')>
<!--- is the argument field an actual field passed to us for validation? --->
<cfset local.argLabel = getLabel(arguments.argument) />
<cfif structKeyExists(variables.rc, arguments.argument) && local.argLabel NEQ "">
<cfset local.message = Replace(local.message, '[ARGUMENT_VALUE]', variables.rc[arguments.argument], 'all') />
<cfset local.message = Replace(local.message, '[ARGUMENT]', local.argLabel, 'all') />
<cfelse>
<cfset local.message = Replace(local.message, '[ARGUMENT]', arguments.argument, 'all') />
</cfif>
<!--- include value in the message name? --->
<cfif structKeyExists(arguments, 'value')>
<cfset local.message = Replace(local.message, '[VALUE]', arguments.value, 'all') />
</cfif>
<cfset addError(arguments.field, local.message) />
</cffunction>
<!--- add error messages to the global array --->
<cffunction name="addError" output="false" access="public" returntype="void">
<cfargument name="field" required="true" type="string">
<cfargument name="message" required="true" type="string">
<cfset arrayAppend(variables.errors, {field=arguments.field, message=arguments.message})>
<!--- keep track of the total amount of errors each field has --->
<cfif NOT structKeyExists(variables.errorFields, arguments.field)>
<cfset variables.errorFields[arguments.field] = 0 />
</cfif>
<cfset variables.errorFields[arguments.field] = variables.errorFields[arguments.field] + 1 />
</cffunction>
<cffunction name="getErrorMessage" output="false" access="private" returntype="string">
<cfargument name="field" required="true" type="string">
<cfargument name="method" required="false" type="string">
<cfset var local = {}>
<cfif structKeyExists(arguments, 'method') AND structKeyExists(variables.settings.errors.unique, arguments.field) AND structKeyExists(variables.settings.errors.unique[arguments.field], arguments.method)>
<cfset local.message = variables.settings.errors.unique[arguments.field][arguments.method]>
<cfelseif structKeyExists(variables.settings.errors, arguments.method)>
<cfset local.message = variables.settings.errors[arguments.method]>
<cfelse>
<cfset local.message = 'Unable to locate an error message for method ' & arguments.method & ' for field: [LABEL]'>
</cfif>
<cfreturn local.message>
</cffunction>
<!--- return the struct of fields that have errors and the total count of errors --->
<cffunction name="getErrorFields" output="false" access="public" returntype="struct">
<cfreturn variables.errorFields>
</cffunction>
<!--- returns a list of all fields that have an error present --->
<cffunction name="getErrorFieldList" output="false" access="public" returntype="string">
<cfreturn structKeyList(variables.errorFields) />
</cffunction>
<!--- returns how many errors are set on a specific field --->
<cffunction name="fieldErrorCount" access="public" outuput="false" returntype="numeric">
<cfargument name="field" required="true" type="string" />
<cfset local.errors = 0 />
<cfif structKeyExists(variables.errorFields, arguments.field)>
<cfset local.errors = variables.errorFields[arguments.field] />
</cfif>
<cfreturn local.errors />
</cffunction>
<!--- returns the total errors present on a given list of fields --->
<cffunction name="fieldListHasError" access="public" output="false" returntype="boolean">
<cfargument name="fieldList" required="true" type="string" />
<cfset var local = structNew() />
<cfset local.errors = 0 />
<cfloop list="#arguments.fieldList#" index="local.field">
<cfset local.errors = local.errors + fieldErrorCount(local.field) />
</cfloop>
<cfreturn local.errors GT 0 />
</cffunction>
<cffunction name="getFieldLabel" output="false" access="public" returntype="string">
<cfargument name="field" required="true" type="string">
<cfif structKeyExists(variables.validation,arguments.field) AND structKeyExists(variables.validation[arguments.field], 'label')>
<cfreturn variables.validation[arguments.field].label>
</cfif>
<cfreturn ''>
</cffunction>
<cffunction name="importRoutines" output="true" access="public" returntype="void">
<cfargument name="filename" required="true" type="string">
<cfset var local = {}>
<cfif fileExists(expandPath(arguments.filename))>
<!--- Since we are going to include the UDF/CFC, there is a chance that
the syntax is malformed or the UDF already exists in scope and we
don't want that to throw errors and kill the whole validation. --->
<cftry>
<cfif listLast(getFileInfo(expandPath(arguments.filename)).name, ".") EQ "cfm">
<!--- We don't need to call setMethod() on CFM includes as the functions are
automatically added to the "variables" scope.
** Excluded for now since I would rather not include files without control
of what is being executed. **
<cfinclude template="#arguments.filename#">--->
<cfelseif listLast(getFileInfo(expandPath(arguments.filename)).name, ".") EQ "cfc">
<!--- Now we create a reference to the supplied CFC, get a list of its methods,
and use only those that return "boolean" since that's what the validator
looks for. --->
<cfset arguments.filename = replaceNoCase(arguments.filename, ".cfc", "", "all")>
<cfset local.cfc = createObject("component", "#getComponentMetaData(arguments.filename).name#")>
<cfset local.arMethods = getComponentMetaData(arguments.filename).functions>
<!--- Add in the methods to this validator --->
<cfloop array="#local.arMethods#" index="local.stcMethod">
<cfif structKeyExists(local.cfc, local.stcMethod.name) AND local.stcMethod.returnType EQ "boolean">
<cfset setMethod(local.stcMethod.name, local.cfc[local.stcMethod.name])>
</cfif>
</cfloop>
</cfif>
<cfcatch type="any">
<cfrethrow />
</cfcatch>
</cftry>
</cfif>
</cffunction>
<cffunction name="getErrorMessages" access="public" output="false" returntype="struct"
hint="Returns a collection of all set error messsages">
<cfset var stc = {}>
<cfloop collection="#variables.settings.errors#" item="routine">
<cfif isSimpleValue(variables.settings.errors[routine])>
<cfset stc[routine] = variables.settings.errors[routine]>
</cfif>
</cfloop>
<cfreturn stc>
</cffunction>
<cffunction name="hasErrorMessage" access="public" output="false" returntype="boolean"
hint="Checks to see if a custom error message has been set for a specific field and routine">
<cfargument name="routine" required="true" type="string">
<cfargument name="field" required="true" type="string">
<cftry>
<cfset len(variables.settings.errors.unique[arguments.field][arguments.routine])>
<cfreturn true>
<cfcatch type="any">
<cfreturn false>
</cfcatch>
</cftry>
</cffunction>
<!--- All validation routines follow. Must return true for succesful
validation or false for failed validation; every function gets passed the following arguments:
value = the value of the current field being validated
argument = the value passed if when setting validation a value was put in brackets ie myroutine[argument_here]
field = the name of the form field currently being validated --->
<cffunction name="required" output="false" access="public" returntype="boolean"
hint="Requires a field, or requires is based on a target field's value">
<cfargument name="value" required="true" type="string">
<cfargument name="argument" required="true" type="string">
<cfargument name="field" required="true" type="string">
<cfset var local = {}>
<!--- If no arguments were passed, just check to see if the field has a value --->
<cfif len(trim(arguments.argument)) EQ 0>
<cfreturn len(trim(arguments.value)) GT 0>
</cfif>
<cfset local.arg = trim(listFirst(arguments.argument, "="))>
<cfset local.val = trim(listLast(arguments.argument, "="))>
<!--- Check for requiring at least X field(s) --->
<cfset local.doCheckAtLeast = reReplaceNoCase(local.arg, "[^a-z]", "", "all") EQ "atleast">
<cfset local.checkAtLeast = isNumeric(reReplaceNoCase(local.arg, "\D", "", "all")) ? reReplaceNoCase(local.arg, "\D", "", "all") : 0>
<!--- Checks that at least X of the specified fields has a value --->
<cfif local.doCheckAtLeast && local.checkAtLeast GT 0>
<cfset local.atLeastCount = 0>
<cfloop list="#local.val#" index="local.j" delimiters="|">
<cfif structKeyExists(variables.rc, local.j) && len(trim(getValue(local.j))) GT 0>
<cfset local.atLeastCount++>
</cfif>
</cfloop>
<cfif local.atLeastCount LT local.checkAtLeast>
<cfif NOT hasErrorMessage("required", arguments.field)>
<cfset local.fieldLabels = []>
<cfloop list="#local.val#" index="local.k" delimiters="|">
<cfset arrayAppend(local.fieldLabels, getFieldLabel(local.k))>
</cfloop>
<cfset setFieldErrorMessage(arguments.field, "required", "You must supply a value for one of the following fields: #arrayToList(local.fieldLabels, ", ")#.")>
<cfreturn false>
</cfif>
<cfreturn true>
</cfif>
<cfelseif structKeyExists(variables.rc, local.arg)>
<cfif listLen(arguments.argument, "=") EQ 2 AND len(trim(local.val)) GT 0>
<cfif variables.rc[local.arg] EQ local.val AND len(trim(arguments.value)) EQ 0>
<cfreturn false>
</cfif>
<cfelse>
<cfif len(variables.rc[local.arg]) GT 0 AND len(trim(arguments.value)) EQ 0>
<cfreturn false>
</cfif>
</cfif>
</cfif>
<cfreturn true>
</cffunction>
<cffunction name="matches" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="string" default="">
<cfargument name="field" required="false" type="string" default="">
<cfset var local = {}>
<cfif structKeyExists(variables.rc, arguments.argument) AND variables.rc[arguments.argument] EQ arguments.value>
<cfreturn true>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="min_length" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfif len(trim(arguments.value)) GTE arguments.argument>
<cfreturn true>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="max_length" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfif len(trim(arguments.value)) LTE arguments.argument>
<cfreturn true>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="exact_length" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfif len(trim(arguments.value)) EQ arguments.argument>
<cfreturn true>
</cfif>
<cfreturn false>
</cffunction>
<!--- comparison function. Used to handle less_than,greater_than,less_than_or_equal_to,greater_than_or_equal_to
and additional date compare functions --->
<cffunction name="compare_values" output="false" returntype="boolean" access="private">
<cfargument name="value" type="string" required="true" />
<cfargument name="operator" type="string" required="true" hint="comparison operator. One of: LT, LTE, GT, GTE">
<cfargument name="argument" type="string" required="true" />
<cfargument name="date" type="boolean" required="true" default="false" hint="type of comparison to run (true for utlizing parseDateTime()" />
<cfset var local = structNew() />
<cfset local.value = arguments.value />
<cfset local.argument = listToArray(arguments.argument, '|') />
<cfset local.argumentField = '' />
<!--- if the first argument value passed is actually a field, use the field value instead of the field name--->
<cfif structKeyExists(variables.rc, local.argument[1])>
<cfset local.argumentField = local.argument[1] />
<cfset local.argument[1] = variables.rc[local.argument[1]] />
</cfif>
<cfif arguments.date>
<cfif isDate(local.value) && isDate(local.argument[1])>
<cfif arrayLen(local.argument) GTE 2>
<cfset local.dateTimeMask = local.argument[2] />
<cfelse>
<cfset local.dateTimeMask = 'mm/dd/yyyy h:nn TT' />
</cfif>
<cfset local.value = parseDateTime(local.value) />
<cfset local.argument[1] = parseDateTime(local.argument[1]) />
<!--- if the field exists in the request collection, update the rc field value with the mask'ed value --->
<cfif local.argumentField NEQ ''>
<cfset variables.rc[local.argumentField] = dateTimeFormat(local.argument[1], local.dateTimeMask) />
</cfif>
<cfelse>
<cfreturn false />
</cfif>
</cfif>
<cfreturn evaluate("local.value #arguments.operator# local.argument[1]") />
</cffunction>
<cffunction name="greater_than" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfset var local = structNew() />
<cfreturn compare_values(arguments.value, 'GT', arguments.argument) />
</cffunction>
<cffunction name="greater_than_datetime" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfset var local = structNew() />
<cfreturn compare_values(arguments.value, 'GT', arguments.argument, TRUE) />
</cffunction>
<cffunction name="greater_than_or_equal_to" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfset var local = structNew() />
<cfreturn compare_values(arguments.value, 'GTE', arguments.argument) />
</cffunction>
<cffunction name="greater_than_or_equal_to_datetime" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfset var local = structNew() />
<cfreturn compare_values(arguments.value, 'GTE', arguments.argument, TRUE) />
</cffunction>
<cffunction name="less_than" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfset var local = structNew() />
<cfreturn compare_values(arguments.value, 'LT', arguments.argument) />
</cffunction>
<cffunction name="less_than_datetime" output="false" returntype="boolean" access="public">
<cfargument name="value" required="false" type="any" default="">
<cfargument name="argument" required="false" type="any" default="">
<cfset var local = structNew() />
<cfreturn compare_values(arguments.value, 'LT', arguments.argument, TRUE) />