reactor  

Re: [Reactor for CF] Guide to validation/dictionary use

Ken Dunnington
Tue, 28 Nov 2006 07:12:39 -0800

Thanks Dan (and Dan!) That's good info, just what I was looking for!

On 11/27/06, Dan Sorensen <[EMAIL PROTECTED]> wrote:
Ken,

I'm no Reactor validation expert, but here's what I've done along the
lines you were asking about. (My email program is making this hard to
read, so it might be best to copy this to a code editor that will
colorize the text for easier reading.)


In my custom courseValidator.cfc (In this case my object was for a
Course business object) I extended the validate function like so:


<cffunction name="validate" access="public" hint="I validate a course
record" output="false" returntype="reactor.util.ErrorCollection">
                <cfargument name="courseRecord" hint="I am the Record to
validate." required="no" type="reactor.project.tprc.Record.CourseRecord"
/>
                <cfargument name="ErrorCollection" hint="I am the error
collection to populate. If not provided a new collection is created."
required="no" type="reactor.util.ErrorCollection"
default="#createErrorCollection(arguments.CourseRecord._getDictionary())
#" />

                <!--- Run standard validation - Calls the automatically
generated tests that check for consistency with the data type --->
                        <cfset super.validate(arguments.courseRecord,
arguments.ErrorCollection) />


                <!--- Run my extended validation checks - These point to
custom validation functions within this cfc --->
                        <cfset valCourseClosed(arguments.CourseRecord,
arguments.ErrorCollection) />
                        <cfset
valUniqueTitleCourseNumber(arguments.CourseRecord,
arguments.ErrorCollection) />
                        <cfset
valUniqueCourseNumber(arguments.CourseRecord, arguments.ErrorCollection)
/>

                <!--- Run additional strict validation if Course Status
is set as: 1 (Publish) - Optionally runs these custom validation
functions --->
                <cfif arguments.courseRecord.getStatus() is 1>
                        <cfset valURLFormat(arguments.CourseRecord,
arguments.ErrorCollection) />
                        <cfset valEmailFormat(arguments.CourseRecord,
arguments.ErrorCollection) />
                        <cfset valPhoneFormat(arguments.CourseRecord,
arguments.ErrorCollection) />
                        <cfset valZipCodeFormat(arguments.CourseRecord,
arguments.ErrorCollection) />
                        <cfset
valDisplayDateOrder(arguments.CourseRecord, arguments.ErrorCollection)
/>
                        <cfset
valDuplicateSessions(arguments.CourseRecord, arguments.ErrorCollection)
/>
                </cfif>

                <cfreturn arguments.ErrorCollection />
        </cffunction>


Next I've created a valURLFormat function and a valEmailFormat function
(Two examples of the of many custom tests referred to in my 'validate'
function above in my courseValidator.cfc):

                <!--- Validate URL Format --->
                <cffunction name="valURLFormat" access="public" hint="I
make sure the location URL is valid." output="false" returntype="any"
_returntype="reactor.util.ErrorCollection">
                        <cfargument name="CourseRecord" hint="I am the
Record to validate." required="no" type="any"
_type="reactor.project.tprc.Record.CourseRecord" />
                        <cfargument name="ErrorCollection" hint="I am
the error collection to populate. If not provided a new collection is
created." required="no" type="any" _type="reactor.util.ErrorCollection"
default="#createErrorCollection(arguments.CourseRecord._getDictionary())
#" />

                        <cfif
len(arguments.CourseRecord.getLocationURL()) AND not isValid("url",
arguments.CourseRecord.getLocationURL())>
                                <cfset
arguments.ErrorCollection.addError("Course.locationURL.invalidURLFormat"
) />
                        </cfif>

                        <cfreturn arguments.ErrorCollection />
                </cffunction>


Example Custom Email Address Validator:

                <!--- Validate Email Format --->
                <cffunction name="valEmailFormat" access="public"
hint="I make sure the contact email is a valid format." output="false"
returntype="any" _returntype="reactor.util.ErrorCollection">
                        <cfargument name="CourseRecord" hint="I am the
Record to validate." required="no" type="any"
_type="reactor.project.tprc.Record.CourseRecord" />
                        <cfargument name="ErrorCollection" hint="I am
the error collection to populate. If not provided a new collection is
created." required="no" type="any" _type="reactor.util.ErrorCollection"
default="#createErrorCollection(arguments.CourseRecord._getDictionary())
#" />

                        <cfif
len(arguments.CourseRecord.getContactEmail()) AND not isValid("email",
arguments.CourseRecord.getContactEmail())>
                                <cfset
arguments.ErrorCollection.addError("Course.contactEmail.invalidEmailForm
at") />
                        </cfif>

                        <cfreturn arguments.ErrorCollection />
                </cffunction>


Related section of my coursedictionary.xml:

<?xml version="1.0" encoding="UTF-8"?>
        <course>
                <locationURL>
                        <label>locationURL</label>
                        <comment/>
                        <maxlength>255</maxlength>
                        <invalidType>The locationURL field does not
contain valid data.  This field must be a string value.</invalidType>
                        <invalidLength>The locationURL field is too
long.  This field must be no more than 255 bytes long.</invalidLength>
                        <invalidURLFormat>Please enter a valid web
address or leave blank.</invalidURLFormat>
                </locationURL>
                <contactEmail>
                        <label>contactEmail</label>
                        <comment/>
                        <maxlength>100</maxlength>
                        <invalidType>The contactEmail field does not
contain valid data.  This field must be a string value.</invalidType>
                        <invalidLength>The contactEmail field is too
long.  This field must be no more than 100 bytes long.</invalidLength>
                        <invalidEmailFormat>Please enter a valid email
address or leave blank.</invalidEmailFormat>
                </contactEmail>

...many more items in the dictionary...

        </course>

The errors I throw in the custom validator correspond to my custom lines
in the related dictionary. This way I can change my error text without
having to open up the validator CFC again.

I hope this helps a little bit,
Dan

-----Original Message-----
From: [EMAIL PROTECTED] [EMAIL PROTECTED] On
Behalf Of Ken Dunnington
Sent: Monday, November 27, 2006 1:24 PM
To: reactor@doughughes.net
Subject: [Reactor for CF] Guide to validation/dictionary use

I'm looking for some info and good examples on using and customizing
the validation in Reactor. Things like making fields optional, adding
regex tests for email or urls, etc. I couldn't find much in the list
archives, and haven't had much luck online either. I don't have a
specific problem, I'm just polishing up my skill set.

Thanks in advance,
 Ken

--
Suppose you were an idiot. And suppose you were a member of congress.
But I repeat myself.
 -- Mark Twain


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- --
Reactor for ColdFusion Mailing List
reactor@doughughes.net
Archives at: http://www.mail-archive.com/reactor%40doughughes.net/
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- --



-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Reactor for ColdFusion Mailing List
reactor@doughughes.net
Archives at: http://www.mail-archive.com/reactor%40doughughes.net/
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --




--
Suppose you were an idiot. And suppose you were a member of congress.
But I repeat myself.
-- Mark Twain


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Reactor for ColdFusion Mailing List
reactor@doughughes.net
Archives at: http://www.mail-archive.com/reactor%40doughughes.net/
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --