简体   繁体   中英

get form field value from collection loop

I am looping over a collection (form) and testing for 'attachedFile' in the form field name. On success, I want to add the form field value to an array. Currently, I am only getting the form field name and not the value.

<cfloop collection="#FORM#" item="field">
    <cfif FindNoCase('attachedFile',field) IS 1>
        <cfset fileNamesArray[fileNamesIndex] = field>
        <cfset fileNamesIndex = fileNamesIndex + 1>
    </cfif>
</cfloop>

I tried setting the array at index [whatever] to #form.field# but that results in an error (undefined). Any ideas how to get my value inside this loop? Thanks.

<cfloop collection="#Form#" item="field">
    <cfset currentFieldName  = field>
    <cfset currentFieldValue = Form[field]>
</cfloop>

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fe2.html

Or if you prefer the script style, and you're using CF9, use the for-in loop

<cfscript>
    for (field in Form)
    {
        currentFieldName  = field;
        currentFieldValue = Form[field];
    }
</cfscript>

In Coldfusion 10 or Railo 4, you can use the filter() function of the Underscore.cfc library in cfscript like so:

var fileNamesArray = _.filter(form, function (value, field) {
    return FindNoCase('attachedFile', field);
});

The filter() function returns an array of values that pass the truth test, which in this case is FindNoCase(...).

Using functional style programming results in a more elegant and expressive solution.

(Note: I wrote Underscore.cfc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM