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.)
Archived Comments
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.
I'm not surprised because in ColdFusion
#Listlen('a,b,,d')# is equal to 3.
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.
@Phillip: It was - but CF added the ability to count null items back in 8 I believe.
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....
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...
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.
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. :\
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.
The last comment by Adobe said "Yes, we will make it available in the next update for CF 10." Can anyone confirm this?
I can confirm that it is still not fixed in CF10. I updated the bug.
Do you know if this was resolved in CF11?
I linked to the bug report - you can see yourself. :)