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

StructCount bug when deleting a key from the FORM scope

New Here ,
Jun 29, 2009 Jun 29, 2009

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.

1.5K
Translate
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

correct answers 1 Correct answer

Valorous Hero , Jun 29, 2009 Jun 29, 2009

Unfortunately, I do not think you can avoid it as it seems to be an issue with the underlying FormScope class.  You might wrap up your alternative "count" code in a cffunction.  That would at least keep the logic in one place:

Translate
Valorous Hero ,
Jun 29, 2009 Jun 29, 2009

Can you post a small code snippet instead? The attachment is still showing as "QUEUED".

Translate
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 ,
Jun 29, 2009 Jun 29, 2009

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>

Translate
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
Valorous Hero ,
Jun 29, 2009 Jun 29, 2009

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>

Translate
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
Valorous Hero ,
Jun 29, 2009 Jun 29, 2009

Though less ideal, you could also obtain the correct count using the structure keys directly, instead of using StructCount(...)

        ArrayLen = #ArrayLen(StructKeyArray(FORM))#<br>
        ListLen = #ListLen(StructKeyList(FORM))#<br>

Translate
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 ,
Jun 29, 2009 Jun 29, 2009

Thanks, I had thought of doing something like that--just have a lot of code where this is being used that would need correcting.  Both of your answers are quite helpful though.

Translate
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
Valorous Hero ,
Jun 29, 2009 Jun 29, 2009

Unfortunately, I do not think you can avoid it as it seems to be an issue with the underlying FormScope class.  You might wrap up your alternative "count" code in a cffunction.  That would at least keep the logic in one place:

Translate
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 ,
Jun 29, 2009 Jun 29, 2009

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!

Translate
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
Valorous Hero ,
Jun 29, 2009 Jun 29, 2009
LATEST

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!

Translate
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