ColdFusion and Form Fields with the Same Name

This post is more than 2 years old.

Many months ago I blogged about a great new feature in ColdFusion 10. It allowed you to take form fields with the same name and return them as an array. Why was this important? Consider the following example.

Imagine you've got 2 form fields called "name". You enter "John,Smith" for the first one and "Joe" for the second. When you output form.name you get: "John,Smith,Joe". Ok, so tell me - what value was used for the first name? (Ok, so I just told you, but pretend you don't know.) You can't tell because ColdFusion returns all the value as a list and it just so happens that the first value was also a list.

ColdFusion 10 fixes this by letting you adding this.sameformfieldsasarray=true; to your Application.cfc file. Now when you submit the form you get:

Perfect. Until you add another form field. Now imagine we have 3 name fields. Tell me, what do you expect this returns?

If you said, "An array where element 1 is John,Smith and element 3 is Joe", you would be incorrect. Nope, you get this instead:

Even though the second name field had a value (an empty string is a value!), the array is "collapsed". This is not what I expected and I'd imagine this is not what most people expect. A bug report has been filed for this - 3560964.

Unfortunately, it is the opinion of the engineering team that this makes sense, and matches backwards compatibility in terms of lists. I strongly disagree. But I hashed out my arguments in the bug report and I do not believe there is any chance of it changing. So whatever. The work around is to not use this feature and instead use the Java call: getPageContext().getRequest().getParameterMap().

I suggest not using this.sameFormFieldsAsArray as I think it will mislead you (or your coworkers too cool to read this blog, and others) into thinking that you will get one response when you get another. Just use the Java call and skip the Form scope. (Again, if you are wanting to use form fields with the same name.)

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 Gareth Arch posted on 2/25/2014 at 7:23 PM

Yeah, that doesn't make sense. There *is* data for the 2nd form field, it's just [empty string]. No need to remove that from what is passed, let the user handle what should be done with it.

Comment 2 by Phillip Senn posted on 2/25/2014 at 8:41 PM

I'm not surprised because in ColdFusion
#Listlen('a,b,,d')# is equal to 3.

Comment 3 by Dan G. Switzer, II posted on 2/25/2014 at 8:45 PM

I voiced my opinion in the bug to back you up. Hopefully if they hear enough people tell them they're wrong, they'll rethink things.

Comment 4 by Raymond Camden posted on 2/25/2014 at 8:48 PM

@Phillip: It was - but CF added the ability to count null items back in 8 I believe.

Comment 5 by Ryan Stille posted on 2/25/2014 at 11:33 PM

I didn't think getParameterMap() worked in CF10? I know CF10 broke some of my stuff that used it. I think even in CF9 it didn't work on forms that were multipart/form-data encoded.

I wrote a function that will return form field values as an array, it works on CF10 (probably most other versions too) and it works on either form encoding: http://www.stillnetstudios....

Comment 6 by Ryan Stille posted on 2/25/2014 at 11:51 PM

More on getParameter() and getParameterMap() being broken in CF10. Adobe closed the bug and said they aren't going to fix it.
https://bugbase.adobe.com/i...

Railo had the same issue, but they had it fixed within hours of the bug report being filed. ARG.
https://issues.jboss.org/br...

Comment 7 by Scott Busche posted on 2/26/2014 at 12:45 AM

Wouldn't the easiest solution/workaround be to make the field names unique? e.g. name1, name2, name3? Guess I've always worked under the assumption they needed to be unique and have never ran into this issue.

Comment 8 by Raymond Camden posted on 2/26/2014 at 12:46 AM

That is a solution as well, but it makes parsing a bit more complex imo. The thing is - this (the addition of the sameFormFieldsAsArray) was *supposed* to make this easier. :\

Comment 9 by Raymond Camden posted on 2/28/2014 at 5:27 PM

Well, Good news/bad news. It looks like it IS going to be fixed, but if I had to guess it would be CF11 only. We will see.

Comment 10 by Derek posted on 9/22/2014 at 5:36 PM

The last comment by Adobe said "Yes, we will make it available in the next update for CF 10." Can anyone confirm this?

Comment 11 by Derek posted on 9/22/2014 at 5:53 PM

I can confirm that it is still not fixed in CF10. I updated the bug.

Comment 12 by Robin posted on 7/15/2016 at 7:52 PM

Do you know if this was resolved in CF11?

Comment 13 (In reply to #12) by Raymond Camden posted on 7/16/2016 at 1:17 AM

I linked to the bug report - you can see yourself. :)