New getFieldLengths method for DataMgr

October 28, 2008

Steve Bryant has released DataMgr 2.2 Beta 2 recently, which includes several new features and enhancements. One of the new features is getFieldLengths(). So what is this method for?

When I build forms I like to validate the string length of the submitted data before I submit it to the database. For example here is an simple form to sign up to a email mailing list.

<cfif StructKeyExists( form, "email" )>
  <!--- validate form data --->
  <cfif Len( form.name ) gt 50>
    <cfset variables.message = "The text you have entered for your name is too long. The maximum length is 50 characters." />
  <cfelseif Len( form.email ) gt 50>
    <cfset variables.message = "The text you have entered for your email is too long. The maximum length is 50 characters." />
  <cfelseif Not IsValid( 'email', form.email )>
    <cfset variables.message = "Please check your email address." />
  <cfelse>
    <!--- insert into the database --->
    <cfquery name="InsertSubscriber" datasource="aliaspooryorik">
      INSERT INTO subscribers (
          name
        , email
      )
      VALUES (
        <cfqueryparam value="#form.name#" cfsqltype="cf_sql_varchar" maxlength="50" />
        , <cfqueryparam value="#form.email#" cfsqltype="cf_sql_varchar" maxlength="50" />
      )
    </cfquery>

    <cfset variables.message = "You have been subscribed to our mailing list." />
  </cfif>
<cfelse>
  <!--- set default values --->
  <cfset form.name = "" />
  <cfset form.email = "" />
</cfif>

<h1>Newsletter Signup</h1>

<cfif StructKeyExists( variables, "errormessage" )>
  <h2>#variables.message#</h2>
</cfif>

<form action="self.cfm" method="post">
  Your name: <input type="text" name="name" value="#HtmlEditFormat( form.name )#" maxlength="50" /><br />
  Your email: <input type="text" name="email" value="#HtmlEditFormat( form.email )#" maxlength="50" /><br />
  <input type="submit" value="Subscribe" />
</form>

OK, you'll see that I'm checking the length of the form.email value in my ColdFusion validation and I also have a maxlength attribute on the text box in the HTML and on my cfqueryparam tag. Pretty basic stuff, but what happens if you need to allow the email address to be 150 characters? In the above example we only need to change it in four places, but if we had that form field in multiple places in our application (such as an admin system, user account sign up, edit account details) and it would be a tedious task to edit each script. Enter DataMgr's getFieldLengths method!

First we need to set up DataMgr, typically this is in the Application Scope, so our Application.cfc onApplicationStart method will look something like this:

<cffunction name="onApplicationStart">
  <cfset Application.DataMgr = CreateObject( "component", "my.cfc.path.DataMgr" ).init( datasource="aliaspooryorik" ) />
</cffunction>

Our newsletter signup script will now look like:

<!--- get a struct for database fields and their maximum lengths --->
<cfset fieldLength = Application.DataMgr.getFieldLengths( tablename="subscribers" ) />

<cfif StructKeyExists( form, "email" )>
  <!--- validate form data --->
  <cfif Len( form.name ) gt fieldLength.name>
    <cfset variables.message = "The text you have entered for your name is too long. The maximum length is #fieldLength.name# characters." />
  <cfelseif Len( form.email ) gt fieldLength.email>
    <cfset errormessage = "The text you have entered for your email is too long. The maximum length is #fieldLength.email# characters." />
  <cfelseif Not IsValid( 'email', form.email )>
    <cfset errormessage = "Please check your email address." />
  <cfelse>
    <!--- insert into the database --->
    <cfset Application.DataMgr.SaveRecord( tablename="subscribers", data=form ) />
    <cfset variables.message = "You have been subscribed to our mailing list." />
  </cfif>
<cfelse>
  <!--- set default values --->
  <cfset form.name = "" />
  <cfset form.email = "" />
</cfif>

<h1>Newsletter Signup</h1>

<cfif StructKeyExists( variables, "errormessage" )>
  <h2>#variables.message#</h2>
</cfif>

<form action="self.cfm" method="post">
  Your name: <input type="text" name="name" value="#HtmlEditFormat( form.name )#" maxlength="#fieldLength.name#" /><br />
  Your email: <input type="text" name="email" value="#HtmlEditFormat( form.email )#" maxlength="#fieldLength.email#" /><br />
  <input type="submit" value="Subscribe" />
</form>

If we'd written our code like this, then we'd simply update the database field length to 150 and DataMgr would automatically pull out the new length. Neat huh!

As an extra bonus we can even get DataMgr to set up the default values, although it is worth noting that DataMgr will only return fields that store strings (so it won't return a numeric type field).

<!--- get a struct for database fields and their maximum lengths --->
<cfset fieldLength = Application.DataMgr.getFieldLengths( tablename="subscribers" ) />

<cfif StructKeyExists( form, "email" )>
  <!--- validate form data --->
  <cfif Len( form.name ) gt fieldLength.name>
    <cfset variables.message = "The text you have entered for your name is too long. The maximum length is #fieldLength.name# characters." />
  <cfelseif Len( form.email ) gt fieldLength.email>
    <cfset errormessage = "The text you have entered for your email is too long. The maximum length is #fieldLength.email# characters." />
  <cfelseif Not IsValid( 'email', form.email )>
    <cfset errormessage = "Please check your email address." />
  <cfelse>
    <!--- insert into the database --->
    <cfset Application.DataMgr.SaveRecord( tablename="subscribers", data=form ) />
    <cfset variables.message = "You have been subscribed to our mailing list." />
  </cfif>
<cfelse>
  <!--- set default values --->
  <cfloop collection="#fieldLength#" item="field">
    <cfset form[field] = "" />
  </cfloop>
</cfif>

<h1>Newsletter Signup</h1>

<cfif StructKeyExists( variables, "errormessage" )>
  <h2>#variables.message#</h2>
</cfif>

<form action="self.cfm" method="post">
  Your name: <input type="text" name="name" value="#HtmlEditFormat( form.name )#" maxlength="#fieldLength.name#" /><br />
  Your email: <input type="text" name="email" value="#HtmlEditFormat( form.email )#" maxlength="#fieldLength.email#" /><br />
  <input type="submit" value="Subscribe" />
</form>

I think this feature is extremely useful (which is why I suggested it!) and hope you will as well.

The latest version can be downloaded from Steve's site at:
http://www.bryantwebconsulting.com/cfcs/#DataMgr

The current stable release is available from RiaForge at:
http://datamgr.riaforge.org/


No comments

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.