I Finally Used CFParam Type = "VariableName"

Posted September 26, 2007 at 2:44 PM

Tags: ColdFusion

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 :)

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 26, 2007 at 3:23 PM // reply »
207 Comments

You can also use isValid("variablename", somevar).


Sep 26, 2007 at 3:48 PM // reply »
6,516 Comments

Good call. I don't use IsValid() enough.


Sep 26, 2007 at 3:51 PM // reply »
207 Comments

It was one of the 'gems' overlooked in CF7. Very handy stuff.


Sep 26, 2007 at 3:58 PM // reply »
40 Comments

I go IsValid crazy in ICEGen. IsValid is probably the most useful function ever entered into the ColdFusion family.


Sep 26, 2007 at 3:59 PM // reply »
6,516 Comments

@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.


Sep 26, 2007 at 6:23 PM // reply »
13 Comments

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


Sep 27, 2007 at 7:10 AM // reply »
6,516 Comments

@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.


Post Comment  |  Ask Ben

Recent Blog Comments
aha
Nov 22, 2009 at 7:42 AM
Using A Name Suffix In ColdFusion's CFMail Tag
Why not? ... read »
Nov 22, 2009 at 7:37 AM
Using A Name Suffix In ColdFusion's CFMail Tag
asd ... read »
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »