ColdFusion Muse

Case Insensitive Form Field Names on Submit

Mark Kruger June 14, 2008 4:14 PM Coldfusion Tips and Techniques Comments (3)

Recently on CF Talk Bobby Hartsfield presented an interesting problem. A vendor required a submission of data as an XML packet. Since the data all came from a form post Bobby wanted to simply loop through the form fields to build his XML file. Each of the form field names would become a node in his XML doc. Sounds simple right?

Actually there is a problem with this approach. XML is typically case sensitive. If the vendor uses all upper or all lower case that's not a problem because you could simply UCASE() or LCASE() the field names. But what if the XML node names need to be mixed-case? For example, what if a node name was AdminUserName? You could add a form element to the form called AdminUserName, but when it is posted Coldfusion turns it into all upper case - as in ADMINUSERNAME. It's too bad there is no way to access the original case of the form elements before ColdFusion intervened. As it turns out (and thanks to some nifty code from both Bobby and the very bright Barney Boisvert) there is a way....

It's easy to forget, but you have access to other ways of accessing the values posted to the page. You have access to the underlying context of the post with the little used "getPageContext( )" function. nested down the object tree is a method called getParameterNames() that will give you access to the case-insensitive field names before Coldfusion took charge. To help you out a bit further, here is a function that will return a structure of objects with case insensitive keys from the form.

<Cfscript>
function formWithCase() {
    // object with case insensitve params
    var params = getPageContext().getRequest().getHttpRequest().getParameterNames();
    //placeholder for form element name
    var field = '';
    //object to return
    var retObj = structNew();
    //loop through each form element
    while(params.hasMoreElements()) {
        //this is the case insensitive field elemente
        field = params.nextElement();
        //copy the value to the new case insenstive form struct.
        retObj[field] = form[field];

    }
    return retObj;
}
</cfscript>

To use this function there needs to be form variables in scope. Here's a test script:

<cfif cgi.request_method is "post">
    
    <Cfset formObj = formWithCase()/>
    
    <cfdump var="#formObj#"/>

</cfif>
<hr />
<pre>
<form name="tempForm" action="#cgi.script_name#" method="post">
<input type="text" name="someThingBorrowed" value="" /><br />
<input type="text" name="someThingBlue" value="" /><br />
             <input type="submit" value="go" />
</form>
</pre>
This seemed pretty clever to me - but perhaps some muse reader will come up with an even more elegant solution?

  • Share:

3 Comments

  • Dan G. Switzer, II's Gravatar
    Posted By
    Dan G. Switzer, II | 6/16/08 9:24 AM
    You can also use GetHttpRequestData().content to retrieve the posted original data. It's stored in a amperand delimited value/key pair (just like URL parameters are.)

    The method you list is much easier to parse, but there may be times when you want the raw posted data.
  • Mark Mazelin's Gravatar
    Posted By
    Mark Mazelin | 6/20/08 12:19 PM
    Mark:

    There are some interesting ways that the form scope and the getHttpRequestData().content fields work. In a project I worked on, I used the best of both worlds, similar to how you did. See my <a href="http://www.mkville.com/blog/index.cfm/2007/9/25/Fo... post</a> and the associated first post on it.

    Maz
  • Billigflug's Gravatar
    Posted By
    Billigflug | 9/11/08 9:13 AM
    Great solution, dude. I like it, not only useful but easy to implement, thanks.