ColdFusion CFParam Can Use Struct And Array Notation

Posted July 2, 2007 at 7:00 AM by Ben Nadel

Tags: ColdFusion

During Selene Bainum's CFUNITED presentation about using Structs and CFCs I noticed that she was using the ColdFusion CFParam tag with struct an array notation. I had always thought that the CFParam Name attribute could only take "valid" variable names (those strictly using dot notation), so it was news to me that it could handle stuff like this:

  • <!--- Param the Sessions struct. --->
  • <cfparam
  • name="REQUEST[ 'Sessions' ]"
  • type="array"
  • default="#ArrayNew( 1 )#"
  • />
  •  
  • <cfparam
  • name="REQUEST[ 'Sessions' ][ 1 ]"
  • type="struct"
  • default="#StructNew()#"
  • />
  •  
  • <cfparam
  • name="REQUEST[ 'Sessions' ][ 1 ][ 'Name' ]"
  • type="string"
  • default="Using Structures and CFCs"
  • />
  •  
  • <!--- Dump out param'd scope. --->
  • <cfdump
  • var="#REQUEST#"
  • label="CFParam With Struct And Array Notation"
  • />

CFDumping out the REQUEST scope, we can see that ColdFusion CFParam worked just as we expected, creating both nested struct and arrays:


 
 
 

 
ColdFusion CFParam Tag With Struct And Array Notation  
 
 
 

After the presentation was over, I asked Selene what version of ColdFusion she was running, believing that maybe this was a new feature in ColdFusion 8. She was indeed running ColdFusion 8 on her laptop, but she said this was not a new feature. I am running ColdFusion 7 on my laptop and as you can see from above, it works nicely. She did say that in the past she thinks there were issues with running CFParam on structs that did not yet exist, but that also seems to work fine for me:

  • <!---
  • Param the Event structure. The Event sturcture
  • does not exist yet, so ColdFusion will create
  • it for us.
  • --->
  • <cfparam
  • name="Event[ 'Name' ]"
  • type="string"
  • default="Using Structures and CFCs"
  • />
  •  
  • <!--- Dump out the param'd scope. --->
  • <cfdump
  • var="#Event#"
  • label="CFParam With Non-existing Structs"
  • />

CFDumping out the Event struct, which previously didn't exist, we can see that ColdFusion not only creates the Event struct but also creates the sub-struct properly:


 
 
 

 
ColdFusion CFParam Tag Non-Existent Struct Notation  
 
 
 

Cool stuff.


You Might Also Be Interested In:



Reader Comments

Jul 2, 2007 at 9:09 AM // reply »
321 Comments

I'd recommend against this (specifically the _last_ thing you did). CF has had the ability to 'auto-make' structs for a while, and I've seen issues doing this in the past. I always recommend folks make structs the normal way - by using structNew first. You can do this in a cfparam too of course. The only exception is structget, which no one really uses anyway.


Jul 2, 2007 at 9:13 AM // reply »
321 Comments

For a good example of how it breaks, run your code, and then run it with ?event=1 in the URL.


Jul 2, 2007 at 9:20 AM // reply »
11,314 Comments

@Ray,

Yeah, I am not a fan of auto-struct creation. In fact, I never use it. I think it is actually more dangerous than it is good. But, I still was surprised that this sort of notation worked in CFParam.

Ha ha, yeah, I never thought of that (passing the string value via URL and then trying to treat it as a struct). I haven't even tested it, but I am sure it throws the "try to treat a scalar value as an object with members" error... who doesn't get tired of that one :)


Jul 20, 2007 at 1:39 PM // reply »
11 Comments

What about doing something like the following, given the new CF8 syntax?

<cfparam name="event" type="string" default="{Name: 'Using Structures and CFCs'}">

Is that equally bad? I haven't tried it, but I don't think you would run into an issue appending ?event=1 to your query string.


Jul 20, 2007 at 1:46 PM // reply »
11,314 Comments

@Adam,

You can't put implicit struct notation directly into an attribute :( Unless they fix that by the CF8 product release.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools