title: How to declare dynamic form field defaults inside a Coldfusion CFC?
body: <p>I'm looking for a way to declare form default values dynamically in a CFC, I'm calling via AJAX. The current CFC sends orders, which I need to break down into sub-orders.</p>
  
  <p>I had been using this:</p>
  
  <pre><code> &lt;!--- static defaults ---&gt;
   &lt;cffunction name="Defaults" access="public" returntype="struct" output="false" 
      hint="Assign default values to instance"&gt;
      &lt;cfscript&gt;
          var formDefaults = {
                versenden=""
              , speichern="" 
              ...
          }
      &lt;/cfscript&gt;
      &lt;cfreturn formDefaults /&gt;
  &lt;/cffunction&gt;
  
  &lt;cffunction name="Commit" access="public" returntype="struct" output="false" hint="database handler"&gt;
  
   &lt;!--- add dynamic form fields ---&gt;
   &lt;cfscript&gt;             
      var LOCAL = {};
      variables.defs = THIS.Defaults();
   &lt;/cfscript&gt;
   &lt;cfloop collection="#VARIABLES.Instance.FormData#" item="formField"&gt;
      &lt;cfscript&gt;
          if (   LEFT(formField, 5) EQ "MENGE" 
              OR LEFT(formField, 3) EQ "EAN" 
              OR LEFT(formField, 12) EQ "BESTELL_TEXT" 
              OR LEFT(formField, 10) EQ "BESTELLTYP" 
              ...
            ) {
            variables.defs[formField]="";
          }
      &lt;/cfscript&gt;
  &lt;/cfloop&gt;
  &lt;cfscript&gt;
      structAppend(variables.defs, VARIABLES.Instance.FormData);                  
      LOCAL.Basket = variables.defs;  
  &lt;/cfscript&gt;
  
  ...
  </code></pre>
  
  <p>So I first declare static form fields (single instance only) and then try to dynamically append dynamic form fields to my array, which might be transferred multiple times (<code>MENGE38, MENGE39, MENGE40</code> etc)</p>
  
  <p>While this works ok, I need to add another counting element to my form-names, so I would have to change <code>MENGE</code> to something like <code>counter.MENGE</code> or <code>MENGE.counter</code> which will then send form values like this:</p>
  
  <pre><code>MENGE.1.38
  MENGE.1.40
  MENGE.1.41
  MENGE.2.37
  </code></pre>
  
  <p>With the counter denoting the sub-order, this field is used for.</p>
  
  <p>Problem is, this breaks my dynamic form field declaration and I don't understand why. I'm getting the following errors:</p>
  
  <pre><code>Diagnose: Element MENGE.1 is undefined in a CFML structure referenced as part of an expression.
  </code></pre>
  
  <p><strong>Question:</strong><br>
  Can anyone give me a hint on what the problem might be? Do I have to param the form fields on the HTML page as well (shouldn't have to)? </p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong>:<br>
  Problem was in my validate function, I also need to declare the modifications I did above. The new function looks like this:</p>
  
  <pre><code>&lt;cffunction name="Validate" access="public" returntype="array" output="false" hint="validate form inputs and return an array of faulty field names."&gt;
     &lt;cfscript&gt;
       var LOCAL = {};
       var double = structNew();
       double.form = VARIABLES.Instance.FormData;
       double.criteria = VARIABLES.Instance.Validation;
    &lt;/cfscript&gt;
  
    &lt;!--- add dynamic form fields for validation... I FORGOT TO UPDATE THIS---&gt;
    &lt;cfloop collection="#VARIABLES.Instance.FormData#" item="formField"&gt;
       &lt;cfscript&gt;
          if (   LEFT(formField, 5) EQ "MENGE" 
              OR LEFT(formField, 10) EQ 'BESTELLTYP'
              OR LEFT(formField, 3) EQ "EAN" 
              OR LEFT(formField, 12) EQ "BESTELL_TEXT" 
              ...
          ) {
             VARIABLES.Instance.Validation[formField]="pass";
          }
       &lt;/cfscript&gt;
    &lt;/cfloop&gt;
    &lt;!--- Get error names and type ---&gt;
    &lt;cfinvoke component="form_validate" method="validate_fields" double="#double#" returnvariable="validation_errors"&gt;&lt;/cfinvoke&gt;
    &lt;cfset LOCAL.ErrorMessages = validation_errors /&gt;
    &lt;cfreturn LOCAL.ErrorMessages /&gt;
  </code></pre>
  
  <p>  </p>
  
  <p>Because I did not add the new updated the if-clause in this function, I was getting the error.</p>