Skip to content

Instantly share code, notes, and snippets.

@dgibson666
Created April 5, 2016 20:06
Show Gist options
  • Save dgibson666/dd61213d438117bbe6ecf864dc57b229 to your computer and use it in GitHub Desktop.
Save dgibson666/dd61213d438117bbe6ecf864dc57b229 to your computer and use it in GitHub Desktop.
<!--- Parse form fields of the same name as an array instead of a list, to avoid issues with commas
more info: http://www.stillnetstudios.com/cf-form-array-comma-workaround/
--->
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment