ColdFusion 10 Missing Feature - Form Fields and Arrays

This post is more than 2 years old.

Here is a feature that was missed from the docs that I think folks will be happy about. Credit for this find goes to Joe Zack and I'd like to thank Adam Cameron for bringing it to my attention. (And the proper people have been notified about the doc issue so hopefully the online versions will be updated soon to reflect this.) When posting form fields to a ColdFusion page, any form field with the same name will have its values combined into one variable. So consider the following simple script:

Given input of Ray and Camden, you will get a form variable called NAME with a value of Ray,Camden:

That's all well and good and ColdFusion makes it easy to parse that into separate values with list functions, but what happens when you enter "Camden,Ray" and "Foo"?

At this point, it is impossible to tell what the user really entered. Maybe they had "Camden" as the first value. Maybe it was "Camden,Ray". There is no way to properly tell what was put in each field.

Now - to me - the best fix is to simply use unique form field names. But we don't always have control over that. There is a work around you can use, but it would be nice if it was built in.

Turns out - ColdFusion 10 has a feature just for this. Simply by adding one setting to your Application.cfc file, you can convert these values into arrays automatically:

The new setting, sameformfieldsasarray, will enable this feature and generate the array on submission:

It looks like Scott Stroz actually blogged this way back in February and I (and probably many others) completely forgot it!

There is one small caveat that may or may not matter to you. What do you think happens if you leave the first field empty and enter a second item? You get an array of one item. Now - that may be fine for your needs. But if you really want to know that the first field was blank, you would need to check with getHTTPRequestData as mentioned in my earlier fix.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Brian Swartzfager posted on 6/19/2012 at 4:17 PM

Scott also mentioned it on a CFHour podcast; that's where I remember it from.

Comment 2 by Brian Kotek posted on 6/19/2012 at 6:34 PM

Or you can use my Form Utilities CFC (http://formutils.riaforge.o... which does far more. :-)

I've been trying to get them to build this in for a while but interestingly, the bug tracker issue was marked as a duplicate for the fix you mention here, even though it's definitely not a duplicate! I'll have to pester the dev team...

Comment 3 by Ryan Stille posted on 6/20/2012 at 2:41 AM

Great to see this is built into CF10. I wrote a function that pulls form fields as an array that can be used on older versions of CF. Its on CFlib: http://cflib.org/index.cfm?...

and here is my blog post about it: http://www.stillnetstudios....

I wonder if the CF10 feature handles multipart/form-data POSTs? That was something I had to work around in my function.

Comment 4 by Raymond Camden posted on 6/20/2012 at 6:13 PM

Ryan: Try it and see. ;)

Comment 5 by Ben Nadel posted on 7/8/2012 at 10:33 PM

This will definitely make a particular class of problems a lot easier to solve. No more passing extraneous, hidden elements as additional identifiers for groups of elements.

Comment 6 by Luke Nickerson posted on 9/19/2013 at 12:31 AM

Unfortunately this seems to get more complicated when you have input names with differing cases. Try the following in CF10...
<form method="post">
<input type="text" name="name">
<input type="text" name="NAME">
<input type="text" name="name">
<input type="submit">
</form>
...and the form struct will only have the value from the middle input. What's going on here?

Comment 7 by Raymond Camden posted on 9/19/2013 at 6:17 PM

I'm not seeing this with CF10/latest patch. Are you fully patched?

Comment 8 by Luke Nickerson posted on 9/19/2013 at 7:28 PM

Raymond - Thanks, you're right. It must have been a bug that was fixed in one of the recent updates. If anyone else experiences this in CF10 (before update 9), just do an update to the latest. It's fixed as of update 11.

Comment 9 by Raymond Camden posted on 12/5/2013 at 8:12 AM

So - I just recommended this to a CF noob... and then I remembered the damn "if blank, compact the array" - which totally makes this pointless. He had 3 sets of fields, and really needs them to match up.

Damn.

Comment 10 by Raymond Camden posted on 12/5/2013 at 8:15 AM

Looks like a bug was filed: https://bugbase.adobe.com/i...