Please help. I can't figure out what I'm doing wrong.
When I try to delete a key from the FORM scope, the StructCount for the FORM scope remains unchanged, even though the key does not appear to be in the FORM scope any longer. This does not happen when I create a struct in the VARIABLES scope. I'm attaching the code I used to test this.
Is this a bug with ColdFusion? Is there a fix / patch for this? I'm using Coldfusion Enterprise version 8,0,0,176276.
Here's the code. Thanks.
<cfoutput>
<cfif CGI.REQUEST_METHOD is "POST">
<strong>This doesn't work:</strong><br />
Dump the contents of the form scope:<br />
<cfdump var="#form#" label="form">
StructKeyExists(form, "test2")? #structKeyExists(form, 'test2')#<br />
Show the structCount:
#structCount(form)#<br /><br />
Delete the "test2" key.<br />
<cfset structDelete(form, "test2", "false")>
Dump the contents of the form scope:<br />
<cfdump var="#form#" label="form">
StructKeyExists(form, "test2")? #structKeyExists(form, 'test2')#<br />
Show the structCount:
<b>#structCount(form)#</b><br /><br />
</cfif>
<form action="testStructCount.cfm" method="POST">
<input type="input" name="test" value=""><br />
<input type="input" name="test2" value=""><br />
<input type="submit" value="Submit">
</form>
</cfoutput>
I do not think you are doing anything wrong. You may be running into an issue with the fact that FORM scope is a system object. Technicially it is not the same type of object as a regular structure underneath the hood. So it may be a bug with the form object.
Run the attached code. When you submit the form, notice the difference in object type and how it works if you duplicate(..) the form structure.
FORM object type = coldfusion.filter.FormScope
COPY object type = coldfusion.runtime.Struct
<cfif structKeyExists(FORM, "submt")>
<cfoutput>
<cfset copy = duplicate(FORM)>
FORM object type = #form.getClass().getName()#<br>
Copy object type = #copy.getClass().getName()#<hr>
<cfloop list="#form.fieldNames#" index="key">
<cfset StructDelete(copy, key)>
Deleted key #key#. Count = #StructCount(copy)#<br>
</cfloop>
</cfoutput>
</cfif>
<cfoutput>
<form method="post">
<cfloop from="1" to="10" index="x">
<input type="text" name="field#x#" value="#x#">
</cfloop>
<input type="submit" name="submt">
</form>
</cfoutput>
I think you're right about this being related to the FORM scope being a system object. We might just make an alternate count function as you suggest and substitute it; however, I'm leaning towards correcting all of our code that passes the FORM scope as a struct in the first place. Thanks a million for all your help!
You are welcome. Just bear in mind that duplicate(..) creates a deep copy of the FORM scope. So any changes you make to the "copy" will _not_ be reflected in the FORM scope object. That may or may not be a good thing in your case ;-)
It is definitely the fact that the FORM scope is a different type of object. ie coldfusion.filter.FormScope rather than coldfusion.runtime.Struct. Structures are supposed to be a type of java.util.Map object. According to the javadocs, the size() method is supposed to return the number of keys in the "structure".
http://java.sun.com/javase/6/docs/api/java/util/Map.html#size()
If you use the undocumented size() method on both objects, you will notice the FORM object's size() does not change, whereas the size() of the copy object does:
<cfloop list="#form.fieldNames#" index="key">
<cfset StructDelete(COPY, key)>
<cfset StructDelete(FORM, key)>
Deleted key #key# from COPY. Count = #StructCount(COPY)#<br>
Deleted key #key# from FORM. Count = #StructCount(FORM)#<br>
COPY Size = #copy.size()#<br>
FORM Size = #FORM.size()#<br>
</cfloop>
Good luck!
North America
Europe, Middle East and Africa
Asia Pacific
Copyright © 2012 Adobe Systems Incorporated. All rights reserved.
Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).