csm232s
csm232s

Reputation: 1660

ColdFusion Form Array with comma in variable

Currently, there are checkboxes within a form, and the value of the selected checkboxes are stored in a DB when the form is submitted.

<td><input type="Checkbox" name="valueList" value="Some value, with comma"   >Some value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Another Value, with comma"   >Another value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Yet another value"   >Yet another value</td>

However, the issue is with the commas as the current logic uses a list to store these values. So Some value, with comma is inserted as Some value and with comma. The current list is created with the following:

<cfif isDefined("valueList")>
<cfset a=listlen(valueList)>

And the code continues by looping through the list. This is the only reference to valueList I could find in the code. Is there a way to convert this to an Array without the commas becoming an issue?

Upvotes: 3

Views: 3823

Answers (3)

Ryan Stille
Ryan Stille

Reputation: 1364

Actually there is a way to retrieve the data as an array. If your enctype is application/x-www-form-urlencoded (the default enctype) then you only need one line:

<cfset myArray = getPageContext().getRequest().getParameterValues('my_form_or_url_field_name')>

If your enctype is multipart/form-data (this is the type you use when uploading files) then things are a little more complicated. Here is a function I wrote that will return form & url values with a given name as an array, for either enctype:

<cffunction name="FormFieldAsArray" returntype="array" output="false" hint="Returns a Form/URL variable as an array.">
    <cfargument name="fieldName" required="true" type="string" hint="Name of the Form or URL field" />

    <cfset var tmpPartsArray = Form.getPartsArray() />
    <cfset var returnArray = arrayNew(1) /> 
    <cfset var tmpPart = 0 />
    <cfset var tmpValueArray = "" >

    <!--- if the getPartsArray method did not return NULL, then this is a multipart/form-data request, which must be handled as such. --->
    <cfif IsDefined("tmpPartsArray")>
        <cfloop array="#tmpPartsArray#" index="tmpPart">
            <cfif tmpPart.isParam() AND tmpPart.getName() EQ arguments.fieldName>
                <cfset arrayAppend(returnArray, tmpPart.getStringValue()) />
            </cfif>
        </cfloop>
    </cfif>

    <!--- Add the values that maybe on the URL with the same name, also if this *wasn't* a multipart/form-data request then
    the above code did not get any of the data, and the method below will return all of it. --->
    <cfset tmpValueArray = getPageContext().getRequest().getParameterValues(arguments.fieldName) />

    <!--- that may have returned null, so need to test for it. --->
    <cfif IsDefined("tmpValueArray")>
        <cfloop array="#tmpValueArray#" index="tmpPart">
            <cfset arrayAppend(returnArray, tmpPart) />
        </cfloop>
    </cfif>

    <cfreturn returnArray />
</cffunction>

Upvotes: 9

Stefano D
Stefano D

Reputation: 958

The pattern I use is replacing comma (,) with tilde (~) since it's not used in our domain at all, you can use any character you want.

<td><input type="Checkbox" name="valueList" value="Some value~ with comma"   >Some value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Another Value~ with comma"   >Another value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Yet another value"   >Yet another value</td>

So when the form comes over it'll be as follows:

form.valueList = "Some value~ with comma, Another Value~ with comma, Yet another value";

This is the code to get the array you want:

<cfscript>
  variables.myArrayList = ListToArray(form.valueList);
  for(i=1; i LTE ArrayLen(variables.myArrayList); i=i+1)
  {
    variables.myArrayList[i] = ReplaceNoCase(variables.myArrayList[i],"~",",","all");
  } 
</cfscript>

Upvotes: 1

Leigh
Leigh

Reputation: 28873

A delimiter (in this case a comma) should not be present within the data, because then it is near impossible to identify where one element begins and another ends. The best solution is to use something else for the checkbox value. For example, if the descriptions are from a database table, use the numeric record ID instead of the long text description. Then this would be a non-issue.

Upvotes: 1

Related Questions