ValidateThis – simple breakdown to using for a newbie

April 18, 2011 by Mike Henke    No Comments
Posted under: ColdFusion

I have been a fan of ValidateThis (VT) for years but never had a chance to implement it yet at work. I have a newer developer under me and one of his task is to get VT working on a simple form. The sample demos for VT are numerous and the documentation is outstanding. It is so plentify, it might be overwhelming for a newbie. This morning, I helped my co-worker setup a simple form and we wrote down the steps to implement VT.

Step 1

1) create validatethis object most likely in application.cfc/cfm

<cfif NOT StructKeyExists(application, "ValidateThis")>
<cfset ValidateThisConfig = {JSRoot="../js/"}/>
<cfset application.ValidateThis = createObject("component", "ValidateThis.ValidateThis").init(ValidateThisConfig)/>
</cfif>

Step 2

2) on the page we want validated pass in the correct validation requirements (objecttype is xml file, theobject is what is being validated, context is like registration, add, update)

a) server side after form submitted

<!--- Use the validate() method to perform server-side validations on an object. --->
<cfset result = application.ValidateThis.validate(objectType="User",theObject=form,Context=Form.Context) />

b) client side on the form to be submitted

<!--- Use the getInitializationScript() method to return JavaScript code to set up client-side validations. --->
<cfset ValInit = application.ValidateThis.getInitializationScript() />
<cfhtmlhead text="#ValInit#" />

<!--- Use the getValidationScript() method to return JavaScript code for client-side validations.
(objecttype is xml file, context is like registration, add, update)--->
<cfset ValidationScript = application.ValidateThis.getValidationScript(objectType="User",Context=Form.Context) />
<cfhtmlhead text="#ValidationScript#" />

Update

After posting this, I found ValidateThis QuickStart Guide which is a simple breakdown also.