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

Posted on 2006-03-28
Medium Priority
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

Plucka earned 1200 total points
ID: 16317595

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


Expert Comment

ID: 16317830
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

ID: 16322432
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

850 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