Twitter: raymondcamden


Address: Lafayette, LA, USA

ColdFusion and Form Fields with the Same Name

02-25-2014 2,847 views ColdFusion 9 Comments

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

9 Comments

  • Commented on 02-25-2014 at 8:23 AM
    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.
  • Commented on 02-25-2014 at 9:41 AM
    I'm not surprised because in ColdFusion
    #Listlen('a,b,,d')# is equal to 3.
  • Commented on 02-25-2014 at 9:45 AM
    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.
  • Commented on 02-25-2014 at 9:48 AM
    @Phillip: It was - but CF added the ability to count null items back in 8 I believe.
  • Commented on 02-25-2014 at 12: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.com/coldfusion-formfiel...
  • Commented on 02-25-2014 at 12: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/index.cfm?event=bug&...

    Railo had the same issue, but they had it fixed within hours of the bug report being filed. ARG.
    https://issues.jboss.org/browse/RAILO-2622
  • Scott Busche #
    Commented on 02-25-2014 at 1:45 PM
    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.
  • Commented on 02-25-2014 at 1:46 PM
    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. :\
  • Commented on 02-28-2014 at 6:27 AM
    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.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty