say no to duplicating server side and client side validation

I don’t know why this never occurred to me until I read Hal Helm’s post (and no one told me about it)..  This an email that I just sent around to the team (hopefully someone will try it out).

Usually when we create a form we need to do:

  1. Client side validation
  2. Server side validation

This is an industry standard practice, however as you can see you will need to write the same validation logic in Javascript (client side) and ColdFusion (server side). And trust me this is a boring and tedious process. I’ve been thinking for some time how can we eliminate the necessity to duplicate logic but still retain the same level of functionality.

I’ve tried this method: writing server side validation in CFSCRIPT and then you can cut and paste as Javascript (still need to do minor tweak on the code), but it’s still not good as you still have the same thing repeated in 2 places.

While reading Hal’s post a couple days ago he described a technique where the validation logic only need to be written once for both server and client side. He was using CFAJAXPROXY (which I haven’t used before), his technique is quite advanced, I don’t’ fully understand how he does it. But at least in similar manner we can use Ajax to do client side validation.

2 types of of form submit:

1. Ajax submit:

Ajax handler call formProcess.cfm - if validation fails this template will return XML with the list of field IDs and error messages – javascript process this xml and display the error messages accordingly. We can create a generic function that can be reused for this purpose.

2. Normal form submit

If Javascript is disabled, the form will still submit to formProcess.cfm anyway. If formProcess.cfm called not from Ajax context, it will return a ColdFusion struct containing error fields and messages.

If we do this, we only need validation logic on formProcess.cfm we don’t need to duplicate that on Javascript.

But I haven’t tried this myself, in theory this should work right? Thoughts?