Skip to main content
New Participant
June 29, 2009
Answered

StructCount bug when deleting a key from the FORM scope

  • June 29, 2009
  • 1 reply
  • 1583 views

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.

    This topic has been closed for replies.
    Correct answer -__cfSearching__-

    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.


    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:

    1 reply

    Inspiring
    June 29, 2009

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

    New Participant
    June 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>

    Inspiring
    June 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>