passing entire form collection to cfargument??? can it be done?  should it be done

Posted on 2006-03-28
Last Modified: 2013-12-24
Hello experts, I've dozens and dozens and dozens of form vars passing to a CFC, can I pass the entire collection to an argument (rather than creating an argument for each var?)  I found some code on this forum, but it a) can't figure out how to make it work and b) not sure if it's "best practice".

<cfset objIndexedAnnuities = createObject("component", "/cfc/indexedAnnuities")/>
<cfset objIndexedAnnuities.updateSubmission(argumentCollection=form)/>

<cffunction name="updateSubmission" access="public" returntype="boolean" output="false" hint="Update Indexed Annuity approval submission">

<!--- can this one argument contain the entire form structure? --->
<cfargument name="formStructure" required="yes" type="struct">

<cfquery datasource="datasource">
UPDATE tableName
SET repName=<cfqueryparam  cfsqltype="string" value="#arguments.repName#"/>,
repNo=<cfqueryparam  cfsqltype="string" value="#arguments.repNo#"/>,
writingRep=<cfqueryparam  cfsqltype="string" value="#arguments.writingRep#"/>,
otherPartiesName=<cfqueryparam  cfsqltype="string" value="#arguments.otherPartiesName#"/>,
ownerSalutation=<cfqueryparam  cfsqltype="string" value="#arguments.ownerSalutation#"/>,
ownerFirstName=<cfqueryparam  cfsqltype="string" value="#arguments.ownerFirstName#"/>,
ownerMiddleName=<cfqueryparam  cfsqltype="string" value="#arguments.ownerMiddleName#"/>,
ownerLastName=<cfqueryparam  cfsqltype="string" value="#arguments.ownerLastName#"/>,
dateOfBirth=<cfqueryparam  cfsqltype="date" value="#arguments.dateOfBirth#"/>,
age=<cfqueryparam  cfsqltype="numeric" value="#arguments.age#"/>,

etc, etc, etc.

Question by:whaleyk
    LVL 18

    Accepted Solution


    A form is a structure.

    <cfargument name="formStructure" required="yes" type="struct" />

    then just pass the variable form in.

    <cfset objIndexedAnnuities = createObject("component", "/cfc/indexedAnnuities")/>
    <cfset objIndexedAnnuities.updateSubmission(form)/>

    Then just access them. as if they were form variables, but using formStructure. in stead of form.

    ie: form.repName would be accessed as formStructure.repName

    LVL 9

    Expert Comment

    I definitely consider this a 'best practice'.  I usually develop my applications so that the ENTIRE logic layer is handled by component functions and only the view layer is handled on an individual page.  I even do this for 'one-offs' (i.e. a query that is only ever accessed by a single page).  There are multiple reasons for this (ease of modification, scalability, modularity, etc.).  However by far the easiest is simply that it allows you to use queries or anything else for that matter within a <cfscript> block.  Because the form structure is already stored in memory by the server upon posting the form (within that request) it doesn't have any downside that I can see to include any of the scopes to your components.  In fact, although if the component is invoked on the calling page you can see the scopes directly (FORM, URL, SESSION, APPLICATION, SERVER, REQUEST,CGI) it is 'bad practice' to referrence them directly from a component - after all... even though you now are using the local web server - you might in the future be using a different viewer (web services for example).  So any time you wish to pass in a scope you can do so by including it in the arguments (as Plucka correctly has provided an example).  You can do this for any scope... (expanded example follows... please give Plucka the points as that answer is correct).

    <cffunction name="getScopedValues" access="public" returntype="struct">
          <cfargument name="argform" required="yes" type="struct" />
                    <cfargument name="argurl" required="yes" type="struct" />
                    <cfargument name="argsession" required="yes" type="struct" />
                    <cfargument name="argapplication" required="yes" type="struct" />
          var ReturnedData = structNew();
                    ReturnedData.FormVals = Arguments.argform;
                    ReturnedData.UrlVals= Arguments.argurl;
                    ReturnedData.SessionVals= Arguments.argsession;
                    ReturnedData.ApplicationVals= Arguments.argapplication;
          <cfreturn ReturnedData >

    <cfdump var="#getScopedValues(Form,Url,Session,Application)#">

    Not particularly useful example since it merely combines the scopes into a single structure and then returns them, but you get the idea.

    Author Comment

    Thank you both so much.  As digicidal says, Plucka, you're right so you get the points.  :-)  
    digicidal, thank you so much for the valuable explanation.  Learning "why" is as important to me now as making this stuff work. I look forward to learning more from you in the future.


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
    When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now