Skip navigation
kforrister
Currently Being Moderated

StructCount bug when deleting a key from the FORM scope

Jun 29, 2009 10:44 AM

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.

Attachments:
  • Currently Being Moderated
    Community Member
    Jun 29, 2009 10:55 AM

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

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 29, 2009 11:16 AM

    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>

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 29, 2009 11:21 AM

    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>

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 29, 2009 11:32 AM

    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:

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 29, 2009 11:59 AM

    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!

    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points