Search code examples
structcoldfusioncfloop

Using dynamic variables with reReplaceNoCase in a cfloop


I'm using a cfloop to insert dynamic form values into a session struct. I use reReplaceNoCase to sanitize the input and am running into an issue trying to use dynamic form field names in the reReplaceNoCase method. I've tried different naming schemes but can't nail down the syntax. In the posted code, FORM.RTchoice[r] is what is failing and shows the error: Element RTCHOICE is undefined in a Java object of type class [Ljava.lang.String;.. How do I get the form fields to increment by "r" correctly?

<cfloop index="r" from="1" to="#APPLICATION.theCount#">
   <cfset a = StructInsert(SESSION.USER_OBJECT, "RTchoice#r#", "#reReplaceNoCase(FORM.RTchoice[r], "[^a-zA-Z0-9.,(\s)-]", "", "all")#", 1)>
    </cfloop>

Solution

  • You could use

    <cfset a = StructInsert(SESSION.USER_OBJECT, "RTchoice#r#", reReplaceNoCase(FORM['RTchoice#r#'], "[^a-zA-Z0-9.,(\s)-]", "", "all"), 1)>

    Or

    <cfset a = StructInsert(SESSION.USER_OBJECT, "RTchoice#r#", reReplaceNoCase(FORM['RTchoice'&r], "[^a-zA-Z0-9.,(\s)-]", "", "all"), 1)>

    Eg: https://cffiddle.org/app/file?filepath=cafebd5c-f4b5-4fc7-93bf-ff81ca97c234/00f6a79c-7f5f-42b2-b567-8a8a371fa8aa/3c7d3229-f65f-4afe-8538-306d98adf25f.cfm