Sunday, October 10, 2010

Coldfusion Form Validation for the Lazy Programmer

I love to program but I don't like to type.  I constantly look for ways to type less but accomplish more.  When I am not using CFWheels I end up having to type a whole lot more.

The other day I came up with this function:

<cfset not_required="billingform,address_2">
<cfloop collection="#form#" item="key">
<cfif not listFindNoCase(not_required,key) and trim(form[key]) is "">
 <cfset variables.e = variables.e & "<li>#ucase(replace(key,'_',' ','all'))# is a required field.</li>">
</cfif>
</cfloop>



The only thing I am not happy about is it doesn't show the error messages in any kind of order, which makes longer form errors look kind of weird, but I don't consider that to be too big of an issue because most of the time a user is only gonna be shown 1 or 2 error messages.

So what do you think?  How could this function be improved upon?