I Finally Used CFParam Type = "VariableName"
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 :)
Reader Comments
You can also use isValid("variablename", somevar).
Good call. I don't use IsValid() enough.
It was one of the 'gems' overlooked in CF7. Very handy stuff.
I go IsValid crazy in ICEGen. IsValid is probably the most useful function ever entered into the ColdFusion family.
@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.
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
@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.