Home Contact Us

n42 Designs

ColdFusion and Flex Web Development

n42 Designs

n42 Designs specializes in custom web development using the Adobe web development platform, ColdFusion and Flex. For web sites, web applications, large and small, let us know how we can help your business succeed!

Search

n42 Designs

Recent Comments

Subscribe

RSS

web development
web development
web development
web development
 

ValidatorCFC

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:

<cfset validator =
createObject('component','com.n42designs.validation.validator').init()
/
>

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:

<cfset testObj =
createObject('component','testObj') /
>

<cfset
testObj.setFirstName('sean') /
>

<cfset testObj.setLastName('coyne')
/
>

<cfset testObj.setEmail('someonesEmail@gmail.com') />
<cfset
testObj.setDOB('12/26/1981') /
>

<cfset testObj.setGPA('3.89')
/
>

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)

<cfcomponent
name="testObj" output="false">

                

<cfproperty name="firstName" required="true" type="string"
rules="noZeroLengthString" invalidMessage="Please provide your first
name"
/>

<cfproperty name="lastName" required="true" type="string"
rules="noZeroLengthString" invalidMessage="Please provide your last
name"
/>

<cfproperty name="email" required="true" type="string"
rules="noZeroLengthString,validEmail" invalidMessage="Please provide a
valid email address"
/>

<cfproperty name="dob" required="true"
type="string" rules="validDate" invalidMessage="Please provide a valid
date of birth"
/>
            
<cfproperty name="gpa"
required="true" type="numeric" rules="validNumber"
invalidMessage="Please provide a valid number for GPA" />
    
                

<cfset variables.firstName = '' />
<cfset variables.lastName = ''
/
>

<cfset variables.email = '' />
<cfset variables.dob = ''
/
>

<cfset variables.gpa = '' />

                

<cffunction name="getFirstName" returntype="string" output="false"
access="public">

<cfreturn variables.firstName
/>

</cffunction>
<cffunction name="getLastName" returntype="string"
output="false" access="public">

<cfreturn variables.lastName
/>

</cffunction>
<cffunction name="getEmail" returntype="string"
output="false" access="public">

<cfreturn variables.email
/>

</cffunction>
<cffunction name="getDOB" returntype="string"
output="false" access="public">

<cfreturn variables.dob
/>

</cffunction>            
<cffunction name="getGPA"
returntype="numeric" output="false" access="public">

<cfreturn
variables.gpa />

</cffunction>        
                

<cffunction name="setFirstName" returntype="void" output="false"
access="public">

<cfargument name="firstName" required="true"
type="string" />

<cfset variables.firstName = arguments.firstName
/
>

<cffunction>
<cffunction name="setLastName" returntype="void"
output="false" access="public">

<cfargument name="lastName"
required="true" type="string" />

<cfset variables.lastName =
arguments.lastName /
>

</cffunction>
<cffunction name="setEmail"
returntype="void" output="false" access="public">

<cfargument
name="email" required="true" type="string" />

<cfset variables.email =
arguments.email /
>

</cffunction>
<cffunction name="setDOB"
returntype="void" output="false" access="public">

<cfargument
name="DOB" required="true" type="string" />

<cfset variables.DOB =
arguments.DOB /
>

</cffunction>
<cffunction name="setGPA"
returntype="void" output="false" access="public">

<cfargument
name="gpa" required="true" type="string" />

<cfset variables.gpa =
arguments.gpa /
>

</cffunction>    
</cfcomponent>

3. Now pass this object to the validator component validate function to perform the validation

ex:

<cfset
validator.validate(testObj) /
>

4. Use the getValid() and getMessages() functions to work with the data.

ex:

<cfoutput>
#validator.getValid()#
</cfoutput>
<cfdump
var="#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:

<cfset validator.init()
/
>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)