Exercise List: Form Data Validation Context In OOP

Posted November 16, 2007 at 7:36 PM

Tags: ColdFusion, Exercise List

Click here to view the current Online Demo

Click here to view / download the current Code Base

Just a quick thought here on form data validation context in object oriented programming (OOP) and ColdFusion; As I was updating my ErrorCollection.cfc to be used by the Service Objects' Valiate() method, I realized that form validation must take into account the context, or rather the intent of the intent of the form. Form data isn't just valid or invalid; it's valid with regards to a desired action as different actions incur different business logic and rules. I don't have to deal with this in my application, as it is quite small, but take, for example, the Joint object. I don't even delete joints from the system, but if I did, I certainly wouldn't want to delete a joint that was associated with an existing exercise as this would corrupt the existing data.

So, zoom out a bit, if we were dealing with a joint, then we would have two different sets of validation logic:

Action: Update Joint

Validation Logic:

  • Is name of joint set.
  • Is name of joint a given length.

Action: Delete Joint

Validation Logic:

  • Is joint associated with an existing exercise.

Since the context is so important, you can't just have one validation method, Validate(), as I was planning to have. You have to have a validation method for each desired mutating CRUD action. I combine Create and Update into a single Save() method, so I would need:

  • ValidateDelete()
  • ValidateSave()

I feel like with every passing hour, form data validation in object oriented programming is proving to be harder and harder! Procedural programming makes this so easy, but not reusable at all (I know, I know, the payoff is there).

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page





Reader Comments

Nov 17, 2007 at 8:17 AM // reply »
36 Comments

I think the availability of OO techniques has gotten many CFers thinking that OO is the "one true path". You can write well-encapsulated, reusable code that isn't OO.


Nov 17, 2007 at 7:51 PM // reply »
7,538 Comments

@Steve,

I am a huge fan of ColdFusion custom tags, user defined functions, and general CFC-ery, but I figured, learning a new technique for something can never hurt. Clearly, there is a big learning curve for stuff like this, and maybe I will find in the end that it's just not for me; but, gosh darn it, I'm going for it :)


Nov 17, 2007 at 8:29 PM // reply »
36 Comments

I'm glad you are! Well worth it just to gain the experience. I love reading about it.

I was only objecting to the line "Procedural programming makes this so easy, but not reusable at all". Procedural programming doesn't guarantee that code can't be reusable.

Cheers!


Nov 18, 2007 at 3:48 PM // reply »
7,538 Comments

@Steve,

Agreed. Not sure what I was thinking when I wrote that :)


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 17, 2010 at 4:13 PM
Testing For NULL Values In A ColdFusion Query Result Set
To get around the empty strings in an UPDATE statement I pretended the variable was a string by enclosing it in single quotes, CAST it to an integer, let it convert empty strings to zeroes, and used ... read »
Mar 17, 2010 at 4:12 PM
Ask Ben: Environment-Based Application.cfc Settings
Ben, those are valid comments. I posted some code that shows how to "worryfree ;o)" copy code and let the Application.cfc sort it out. http://boncode.blogspot.com/2010/03/cf-dynamically-changing-ap ... read »
Mar 17, 2010 at 3:32 PM
Using Appropriate Status Codes With Each API Response
@Marc, For our project it isn't really a requirement, since most (if not all) of our resources are private and related to the logged in user. Anyway, IIRC google does execute javascript before inde ... read »
Mar 17, 2010 at 3:24 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
Objectivism is a form of positivism, and Quantum Mechanics does not fit positivism. One of the postulates of Quantum Mechanics is the impossibility of separating the object of measurement from the me ... read »
Mar 17, 2010 at 2:52 PM
Ask Ben: Blocking WSDL Access In A ColdFusion Application
<cfif structKeyExists( url, "wsdl" ) and not structKeyExists( url, "x")> should fix that ... read »
Mar 17, 2010 at 2:43 PM
Ask Ben: Blocking WSDL Access In A ColdFusion Application
You are correct. You can use HTTPservice RPC model instead and not worry about the wrapper. Not too much harder to do but not as extensible or fun! I wonder if the Flex app could pass an arg after t ... read »
Mar 17, 2010 at 2:40 PM
Using Appropriate Status Codes With Each API Response
Simon, If you're serving up what is essentially a blank page "template", and then getting the actual content for that page using javascript, how on earth are you getting the search engines to see yo ... read »
Mar 17, 2010 at 2:34 PM
Using Appropriate Status Codes With Each API Response
We have a similar setup as Rocky. We have a backend which only exposes REST services. It doesn't output any html, js, jsp, .. . (We are capable of running jsp, groovy, jsf, ruby, jython, etc.. but w ... read »