Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Build Status


Sean Coyne - Apache 2.0 License

Change log:

Edit: Aaron Greenlee ( on 2.26.2009 Validate all values defined by your object's "cfproperty" tag as originally designed, or, selectively validate by passing a struct with keys matching the name of the cfproperty tag.

Changes: When passing a struct to the validate function, validations will only occur if the property name found in the <cfproperty> tag of the object has a matching key in the passed struct. This allows one to selectively validate fields. This may be useful objects where not all the fields were populated (example: an update).


  • This was inspired by this article. I tried to design it to be a bit more reusable.
  • Thanks to Aaron Greenlee for some great enhancements
  • Travis CI code grabbed from FW1


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.

Create an instance of this object. It should not be in any shared scope as its not thread safe. ex:

validator = createObject('component','com.n42designs.validation.validator').init();

Next take whatever data you want to validate and put it in an object that follows some rules. You must use <cfproperty> tags or script property directives 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 or use the implicit accessors provided by ColdFusion or Railo. 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.

testObj = createObject('component','testObj');

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)

component accessors=true {
	property name = "firstName" required = true type = "string" rules = "noZeroLengthString" invalidMessage="Please provide your first name";
	property name = "lastName" required = true type = "string" rules = "noZeroLengthString" invalidMessage="Please provide your last name";
	property name = "email" required = true type = "string" rules = "noZeroLengthString,validEmail" invalidMessage="Please provide a valid email address";
	property name = "dob" required = true type = "string" rules = "validDate" invalidMessage="Please provide a valid date of birth";
	property name = "gpa" required = true type = "numeric" rules = "validNumber" invalidMessage="Please provide a valid number for GPA";

Now pass this object to the validator component validate function to perform the validation to validate all properties defined



Pass this object to the valuator component validate function along with a struct with keys matching the names defined in your cfproperty tags in your obj to validate only fields outlined in your struct

formFields = structNew();
formFields.lastName = true; = true;
formFields.dob = true;
validator.validate(testObj, formFields);

In my applications, I generate this structure using Brian Kotek's FormUtilities

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

<cfdump var="#validator.getMessages()#" />

If you want to reuse this instance run init() function. If you are not going to reuse then this is optional.



Validates a ColdFusion object according to prebuilt or custom rules







No releases published


No packages published