ValidatorCFC
Jul 20
This component validates the data in an object according to custom rules you set up. It is especially created to determine data validity for a data store (DBMS, etc) rather than for business rules, but you can set up your own complex rules. Some may be out of its scope, however.
I just uploaded version 0.1.000. It is very much a work in progress and I welcome feedback.
Take a look and let me know what you think. You can find it at http://validatorCFC.riaforge.org or at the download link below.
Here is a rundown of an example usage:
1. Create an instance of this object. It should not be in any shared scope as its not thread safe.
ex:
2createObject('component','com.n42designs.validation.validator').init()
3/>
2. Next take whatever data you want to validate and put it in an object that follows some rules.
You must use cfproperty tags to define what rules, whether it is required or not, and messages to use.
You must create getters for each property like getFirstName() where FirstName is an example property name
You can specify any number of rules for each property in a comma separated list. Each rule should be a CFC in the rules subfolder and should extend the _rule.cfc base object.
ex:
2createObject('component','testObj') />
3<cfset
4testObj.setFirstName('sean') />
5<cfset testObj.setLastName('coyne')
6/>
7<cfset testObj.setEmail('someonesEmail@gmail.com') />
8<cfset
9testObj.setDOB('12/26/1981') />
10<cfset testObj.setGPA('3.89')
11/>
where testObj is a component defined as follows: (yes I know that my setGPA function allows for setting an obviously numeric property with a string value, this is for example only)
2name="testObj" output="false">
3
4
5<cfproperty name="firstName" required="true" type="string"
6rules="noZeroLengthString" invalidMessage="Please provide your first
7name" />
8<cfproperty name="lastName" required="true" type="string"
9rules="noZeroLengthString" invalidMessage="Please provide your last
10name" />
11<cfproperty name="email" required="true" type="string"
12rules="noZeroLengthString,validEmail" invalidMessage="Please provide a
13valid email address" />
14<cfproperty name="dob" required="true"
15type="string" rules="validDate" invalidMessage="Please provide a valid
16date of birth" />
17<cfproperty name="gpa"
18required="true" type="numeric" rules="validNumber"
19invalidMessage="Please provide a valid number for GPA" />
20
21
22<cfset variables.firstName = '' />
23<cfset variables.lastName = ''
24/>
25<cfset variables.email = '' />
26<cfset variables.dob = ''
27/>
28<cfset variables.gpa = '' />
29
30
31
32<cffunction name="getFirstName" returntype="string" output="false"
33access="public">
34<cfreturn variables.firstName
35/>
36</cffunction>
37<cffunction name="getLastName" returntype="string"
38output="false" access="public">
39<cfreturn variables.lastName
40/>
41</cffunction>
42<cffunction name="getEmail" returntype="string"
43output="false" access="public">
44<cfreturn variables.email
45/>
46</cffunction>
47<cffunction name="getDOB" returntype="string"
48output="false" access="public">
49<cfreturn variables.dob
50/>
51</cffunction>
52<cffunction name="getGPA"
53returntype="numeric" output="false" access="public">
54<cfreturn
55variables.gpa />
56</cffunction>
57
58
59<cffunction name="setFirstName" returntype="void" output="false"
60access="public">
61<cfargument name="firstName" required="true"
62type="string" />
63<cfset variables.firstName = arguments.firstName
64/>
65<cffunction>
66<cffunction name="setLastName" returntype="void"
67output="false" access="public">
68<cfargument name="lastName"
69required="true" type="string" />
70<cfset variables.lastName =
71arguments.lastName />
72</cffunction>
73<cffunction name="setEmail"
74returntype="void" output="false" access="public">
75<cfargument
76name="email" required="true" type="string" />
77<cfset variables.email =
78arguments.email />
79</cffunction>
80<cffunction name="setDOB"
81returntype="void" output="false" access="public">
82<cfargument
83name="DOB" required="true" type="string" />
84<cfset variables.DOB =
85arguments.DOB />
86</cffunction>
87<cffunction name="setGPA"
88returntype="void" output="false" access="public">
89<cfargument
90name="gpa" required="true" type="string" />
91<cfset variables.gpa =
92arguments.gpa />
93</cffunction>
94</cfcomponent>
3. Now pass this object to the validator component validate function to perform the validation
ex:
2validator.validate(testObj) />
4. Use the getValid() and getMessages() functions to work with the data.
ex:
2#validator.getValid()#
3</cfoutput>
4<cfdump
5var="#validator.getMessages()#" />
5. If you want to reuse this instance run init() function. If you are not going to reuse then this is optional.
ex:
2/>


![[RSS]](/blog/template/rss.png)