Have you heard of ValidateThis? It's a free, open-source validation framework for ColdFusion!
ValidateThis is a framework that allows you to define validation rules for your ColdFusion objects in a single place (either an XML file, a JSON file or in ColdFusion code) and will then generate client-side and server-side validations for you.
But if you are like me you usually end up with two sets of rules, one for the server side, and one for the client. Maintenance quickly becomes a chore, validation rules become complicated and eventually something will break or worse yet, slip through your code.
ValidateThis eliminates all those headaches. With a single, simple XML file you define your rules in ONE place and magically your server side AND client side validations are generated. Business rules change? Update the XML file and you are done. Easy.
Installation is well documented on the ValidateThis wiki (the documentation in general is outstanding) but in a nutshell:
- Download the framework.
- Unzip. Either into the webroot, or elsewhere and create a ColdFusion mapping called ValidateThis.
Follow The Rules
Next you need to define your validation rules. Head over to the wiki and check out the Sample Rules Definition File. While the rules can get quite complicated doing simple validation is easy as this:
<property name="faqanswer" desc="answer"> <rule type="required"/> </property>
Once you have your rules defined integrating ValidateThis into your application is easy. There is documentation covering the major frameworks. You can also pass ValidateThis a simple struct for validation. See the docs for details.
I'm using Model-Glue and ColdSpring. In my ColdSpring.xml file I define my service, configuration and validateThis beans like so:
<bean id="modelglue.ValidationService" class="ValidateThis.extras.ModelGlueValidationService"> <constructor-arg name="ValidateThisConfig"><ref bean="ValidateThisConfig" /></constructor-arg> </bean> <bean id="ValidateThisConfig" class="coldspring.beans.factory.config.MapFactoryBean"> <property name="sourceMap"> <map> <entry key="JSRoot"><value>/modelglueextensions/jquery/validate/</value></entry> <entry key="definitionPath"><value>../model/</value></entry> </map> </property> </bean> <bean id="ValidateThis" class="ValidateThis.ValidateThis"> <constructor-arg name="ValidateThisConfig"><ref bean="ValidateThisConfig" /></constructor-arg> </bean>
I'm also using ColdFusion 9 ORM so validating my objects is as easy as creating my object, populating it and passing it to ValidateThis:
<cfset local.faq = EntityNew('faq') /> <cfset local.faq.setFaqQuestion( Trim(arguments.event.getValue('faqQuestion')) ) /> <cfset local.faq.setFaqAnswer( Trim(arguments.event.getValue('faqAnswer')) ) /> <cfset local.result = beans.validateThis.validate( theObject=local.faq ) />
Now we can simply check local.result.hasErrors() and proceed with either saving our object or returning to our form with out errors.
This was a quick introduction... in our next post we'll look at what we need to add to our form to get client side validation.