|By Matt Osbun||
|May 15, 2007 06:30 PM EDT||
Business Rule Validation
Validating input against business rules is generally more in depth than validating data types. It helps to think of this as giving context to your data. A name, address, and date of birth are nothing more than a set of unrelated data. Once you have applied business rules, however, you have a customer. If business rules give your data meaning, business rule validation ensures that your data is given the correct meaning.
Datatype validation objects are much easier to share than business rule validation objects. This sort of validation is very specific to a given task or application. However, since we are breaking validation into two parts, we can rewrite our business rule validation objects as necessary and reuse our datatype validation objects. Object-oriented applications are really nothing more than a group of objects that are communicating with each other. You, the developer, are free to determine which objects will be a part of a given application. Since our validation objects are loosely coupled, meaning one object doesn't rely on another, we can change out objects as we see fit.
As an example, let's assume that in this particular application our customers need to be between the ages of 13 and 18, and they also need to be citizens of the United States. I would create a Validate() method, perhaps in a customer.cfc object, to check to make sure that the birthdate and citizenship fit the specified parameters. Although this has some similarities to our Validate() method above, the differences are worth pointing out. When validating datatypes, the formBean has no idea how the data will be used. It just knows which form the data is to take. A Customer object, however, is not so much concerned with the representation of the data, but whether or not the data presented can be used to create a valid customer for this application.
Putting It All Together
How you put this all together is really up to you and how you construct your applications. Are you a procedural developer who includes a validation template on a form page once a form submission has been detected? No problem. Your validation template might look something like this:
<cfset datatype = CreateObject("component","myApp.formBean").init(argumentcollection=form)>
<cfset customer = CreateObject("component","myApp.customer").init()>
<cfset validationBean = CreateObject("component","myApp.validationBean").init()>
<!--- Do something to display the error messages --->
<!--- No errors. Continue the application --->
Since I use Mach-II for most of my development needs, I would have an event-bean declared in the mach-ii.xml configuration file for this event, and I would use that bean's Validate() method in an event filter, which is where I would also create my validationBean and, if necessary, any object needed for business rule validation.
Form validation isn't one of those tasks that I look forward to in the morning. Not that I particularly dislike it, but it's just one of those chores of Web development. The object then (no pun intended!) is to make form validation as easy, flexible, and reusable as possible without sacrificing accuracy or functionality.
This is one of the strengths of object-oriented programming. Rather than building one monolithic, interdependent application, it's more like building a house with Legos. This allows you to spend less time on development chores, like form validation, and more time deciding how the Legos should fit together for your application.
|Kevin Penny 05/17/07 01:16:50 AM EDT|
Excellent - I've used some validation methods before but this one makes good sense.
My only comment would be to add another method to the validator that would return true/false if there were errors in it like 'HasErrors()' or 'isEmpty()' vs. using the arraylen() - then you could possily change out how you're returning those errors and your checking code won't have to chanage that much. I suppose the code that loops through it would have to be modified if you went from an array to a query type object anyway - but thought I'd mention it.
- Where Are RIA Technologies Headed in 2008?
- The Next Programming Models, RIAs and Composite Applications
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Constructing an Application with Flash Forms from the Ground Up
- Building a Zip Code Proximity Search with ColdFusion
- Personal Branding Checklist
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Has the Technology Bounceback Begun?
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- Cloud People: A Who's Who of Cloud Computing
- Adobe Flex 2: Advanced DataGrid
- Web Services Using ColdFusion and Apache CXF