ColdFusion and Form Fields with the Same Name

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 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 developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA