• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

What is the implication for using cfparam with the variable scoping requirements

New Here ,
Apr 29, 2024 Apr 29, 2024

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:

 

<cfparam name="lbDrawingISN" default="AddDrawing">
<cfparam name="Archived" default="0">
 
<cfif lbDrawingISN is "Archived">
        <cfset archived = 1>
    <cfelseif lbDrawingISN is "Active">
        <cfset archived = 0>
        <cfset lbDrawingISN = "AddDrawing">
    </cfif>

 

form.lbDrawingISN is submitted.

 

If I change the code to:

 

<cfparam name="lbDrawingISN" default="AddDrawing">
<cfparam name="Archived" default="0">
 
<cfif form.lbDrawingISN is "Archived">
        <cfset archived = 1>
    <cfelseif form.lbDrawingISN is "Active">
        <cfset archived = 0>
        <cfset form.lbDrawingISN = "AddDrawing">
    </cfif>

 

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

Views

207

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 29, 2024 Apr 29, 2024

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

Dave Watts, Eidolon LLC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 29, 2024 Apr 29, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 18, 2024 Jul 18, 2024

Copy link to clipboard

Copied

LATEST

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  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation