I am a HUGE fan of ColdFusion's CFParam tag. I use it, I think, in a more extensive way than most people - both to enforce variable existence and to validate its data type. CFParam allows for many useful types of data type validation, including basic data types such as string, numeric, array, and struct. One type that has always struck me as weird was the type "VariableName".
I could never quite figure out why anyone would ever need to enforce a valid ColdFusion variable name. But then, yesterday, when I was working on my ColdFusion custom tag for looping over regular expression pattern matches, I had a light-bulb moment! Many ColdFusion tags (ex. CFFile, CFDirectory, CFImage, CFQuery) let you store returned data into a named variable. For example, when you run a query, the resultant record set gets stored in a variable defined by the Name attribute. All of these attributes (Name, Result, Variable) need to be valid ColdFusion variable names!
That's when all of this realization came rushing to me - any time you need to store data into a user-defined variable (as I was doing in my custom tag), you need to enforce that the value given is actually a valid ColdFusion variable name.
Of course! It's so obvious, now :)
Comments (7) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Java String Buffer Treated As String In ColdFusion (When Needed)
Ask Ben: Pulling SQL Records Based On A List Of IDs
You can also use isValid("variablename", somevar).
Posted by Raymond Camden on Sep 26, 2007 at 3:23 PM
Good call. I don't use IsValid() enough.
Posted by Ben Nadel on Sep 26, 2007 at 3:48 PM
It was one of the 'gems' overlooked in CF7. Very handy stuff.
Posted by Raymond Camden on Sep 26, 2007 at 3:51 PM
I go IsValid crazy in ICEGen. IsValid is probably the most useful function ever entered into the ColdFusion family.
Posted by tony petruzzi on Sep 26, 2007 at 3:58 PM
@Ray,
The one that I have really leeched onto is the IsValid( "email", FORM.email ). I used to write my own email validation function, which would fail occasionally cause people have some crazy email addresses. Luckily, IsValid( "email" ) is so much more reliable.
Posted by Ben Nadel on Sep 26, 2007 at 3:59 PM
I use type = variablename quite a bit actually... It is much better than just using type = string when you are dealing with simple string values (one word values), it also requires that the string is not zero length...
Here's a blog entry I did on this a while back: http://www.petefreitag.com/item/632.cfm
Posted by Pete Freitag on Sep 26, 2007 at 6:23 PM
@Pete,
Cool tip. I never thought of using that to help enforce logic that was necessarily FOR a variable name, but rather for values that mimic those requirements. Sweet idea.
Posted by Ben Nadel on Sep 27, 2007 at 7:10 AM