Copy link to clipboard
Copied
We use cfparam alot in our code to ensure that if a page is loaded and the form variable or url variable has not been sent, then we have a default value to test against. How do I now change the code so that this continues to work. Does this render cfparam useless?
For example:
form.lbDrawingISN is submitted.
If I change the code to:
It seems that would work, but what if the form.lbDrawingISN value is not provided. Then it wouldn't work. Do you change the param taga
<cfparam name="form.lbDrawingISN" default="AddDrawing">
but then wouldn't the scope be variables.form.lbDrawingISN
Thanks
Copy link to clipboard
Copied
One nice thing about CF is its verbosity in error messages and in CFDUMP. I honestly don't know the answer to your question but I think I can make a good guess, and you should be able to test that guess pretty easily. I think if you specify "form" or "url" as prefixes to the variable name in CFPARAM, it'll provisionally create that variable in the specified scope. As to the purpose of using CFPARAM here, I think it's to let you handle variables that come from one of those two scopes without having to specify which one.
Once you've verified it one way or the other, that'll tell you how to respond. You can, again, use CFDUMP to dump the variables in any given scope and see whether they're in the scope or not. Also, you can use CF's error handling to do basically the same thing. Once you've found out, and assuming my guess was correct, you could write a CFML custom tag to handle this for you:
<!---
name: CFFORMURLPARAM
usage: <cfformurlparam name="var_name" default="var_default_value">
CFML custom tag to act as CFPARAM for both form and URL variables without specifying a scope
--->
<cfif structKeyExists(form, var_name)>
<cfset variables[var_name] eq form[attributes.var_name]>
<cfset variables.var_loc eq "form">
<cfelseif structKeyExists(url, var_name)>
<cfset variables[var_name] eq var_value>
<cfset variables.var_loc eq "url">
<cfelse>
<cfset variables[var_name] eq attributes.var_default_value>
</cfif>
(Note: I haven't tested this code, could be full of typos!)
Anyway, once you create this custom tag and put it where CF can automatically find it, you should be able to replace your CFPARAM tags with CFFORMURLPARAM tags with a simple search and replace.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
The code is confusing. You seem to be undecided which form.lbDrawingISN value is the alternative to "Archived".
Is it "Active" or "AddDrawing"? I think you have to choose one.
Copy link to clipboard
Copied
I will start with that it is always good to test code.
This is tested thoroughly:
<cfsilent>
<!---
Name: CFFormUrlParam
Usage: <cf_formurlparam name="myVarName" default="myVarDefault" />
<cfscript>
cf_formurlparam (name="myVarName", default="myVarDefault");
</cfscript>
Description: CFML custom tag to act as CFPARAM for a variable that may be missing a scope (form, URL)
--->
<CFSWITCH expression="#LCase(ThisTag.ExecutionMode)#">
<CFCASE value="start">
<cfif structKeyExists(form, "#attributes.name#")>
<cfparam name="ParamValue" default="#evaluate('form.#attributes.name#')#" >
<cfelseif structKeyExists(url, "#attributes.name#")>
<cfparam name="ParamValue" default="#evaluate('url.#attributes.name#')#" >
<cfelse>
<cfparam name="ParamValue" default="#attributes.default#" >
</cfif>
</CFCASE>
<CFCASE value="end">
<cfset "CALLER.#ATTRIBUTES.Name#" = ParamValue />
</CFCASE>
</CFSWITCH>
</cfsilent>
<!--- test file contents --->
<cf_formurlparam name="myVarName" default="0" />
<!--- <cfscript>
cf_formurlparam (name="myVarName", default="0");
</cfscript> --->
<cfdump var="#variables#">
<cfdump var="myVarName: #myVarName#">
<p>
<form method="post">
<input type="text" name="myVarName" value="2">
<input type="submit">
</form>
</p>
I thought it was super odd that i could not upload the file FormUrlParam.cfm