Determining the original case of form fields in ColdFusion

This post is more than 2 years old.

This question came over Twitter today so I thought I'd address it in a blog. User @brooksfolk asked if there was a way to get the original form field names sent to a ColdFusion file. When you look at form.fieldnames, or just cfdump the Form scope, the values are always uppercased. Consider the following simple form.

<form method="post"> <input type="text" name="naMe"><br/> <input type="text" name="AGE"><br/> <input type="text" name="foo"><br/> <input type="submit"> </form>

<cfdump var="#form#">

As you can see, each of my three fields uses a different mixture of upper and lower case names. But when I view the dump, I see this:

As you can see, everything is now uppercase. I had a hunch though that getHTTPRequestData might have the "real" data and indeed it does. Here is what a dump of that function shows:

Woot - as you can see - both the original form field names and values (which are blank in this case since I didn't bother to type anything in) are in the content key. If I actually type some values in, I get a more realistic version: naMe=cat+man&AGE=moo&foo=mooooo. This looks easy enough to parse. Here is what I came up with:

<cfset content = getHTTPRequestData().content> <cfset formMoreGooder = {}> <cfloop index="pair" list="#content#" delimiters="&"> <cfset name = listFirst(pair, "=")> <cfset value = urlDecode(listLast(pair, "="))> <cfset formMoreGooder[name] = value> </cfloop>

Basically I just treat the data as 2 sets of lists. Each pair is delimited by an ampersand and then each pair itself is split by the equal sign. The values are url encoded but luckily ColdFusion provides a way to reverse that. And here is the result...

Hope this is helpful to someone.

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 Sooraj posted on 11/13/2010 at 11:15 AM

Hi Ray nice post, and a nice thought...
I have a question about processing logic, suppose the formfield contains a field called "company" and if I enter company value in form as "AT&T", will the above logic work? If not how do we make the logic as it allows &...
Thanks

Comment 2 by Brad posted on 11/13/2010 at 11:16 AM

I just needed this *today*. I was writing an interface using Authorize.net's automated billing system. This system was case sensitive, so it wasn't as simple as accepting a form, validate/scrubbing it, then passing it to the service. A full mapping needed to take place.

This function saved me much headache!

Note: I wouldn't allow this, but say you had two form names like Name and name. Referencing them without going further into java methods can only return one of the above...

so, struct.Name or struct.name will only return one of the above. My short tests always yielded the lower case one.

Comment 3 by Sooraj posted on 11/13/2010 at 11:18 AM

Maybe we need the help of Form structure to identify the exact fields and values....

Comment 4 by Rex posted on 11/13/2010 at 11:36 AM

@Sooraj the field values come out encoded so AT&T becomes AT%26T

Comment 5 by Sooraj posted on 11/13/2010 at 1:11 PM

Thanks Rex for pointing it out...

Comment 6 by Raymond Camden posted on 11/13/2010 at 5:12 PM

@Brad: See this blog entry: http://www.coldfusionjedi.c...

Comment 7 by dawesi posted on 11/15/2010 at 7:15 AM

Any idea how to get case sensitive names in multipart/form-data? seems you can get to the names through form.getPartsArray() but they are still uppercased??!

Comment 8 by Raymond Camden posted on 11/16/2010 at 11:46 PM

No idea on that one. If you look at the comments in the post I linked to in the comment above, it looks like a dead end.

Comment 9 by Critter posted on 11/24/2010 at 12:08 AM

Queries return a columnlist all in caps too.. if you serialize that to JSON, and then use the provided columnlist to get to the data.. it doesn't work.. at least in this library I am using for Objective-C.. do you know of any way to preserve the case in a query returned?

Comment 10 by Critter posted on 11/24/2010 at 12:10 AM

Actually.. i just noticed.. it's not the query.. it's the conversion to JSON...

Comment 11 by Raymond Camden posted on 11/24/2010 at 12:14 AM

If you need the case, then just copy it.

result.data = thequery;
result.collist = thequery.columnlist;

return result;
havebeer;

Comment 12 by Critter posted on 11/24/2010 at 4:29 PM

aye.. sometimes the easiest solutions... yeah right there in front of you..

cheers mate..

Comment 13 by Aaron Foss posted on 1/11/2011 at 12:27 AM

If the value is empty, it puts in the field name, not the value. The listlen is 1, not 2 when it's empty (at least on CF9). I added the following check:

[cfif listlen(pair, "=") eq 1]
[cfset value = ""]
[cfelse]
[cfset value = urlDecode(listLast(pair, "="))]
[/cfif]

Comment 14 by Raymond Camden posted on 1/11/2011 at 12:37 AM

Nice find there, Aaron. Thank you.