0
votes

I have a shopping basket which labels hidden inputs with an id based on a database record. So the input might look like this:

<input type="hidden" name="qty12345678" value="5" />

where "12345678" is the id of a record in the basket.

I'm submitting the form with these inputs using AJAX and sending them to a CFC for processing. I usually pre-define all my form field values inside the CFC like so:

<cfcomponent output="false">
<cfscript>
    VARIABLES.Instance.Validation = {
            field_A = "pass"
        , field_B = "pass"
        ...
    }

<cffunction name="Defaults" access="public" returntype="struct" output="false">
    <cfscript>
    var formDefaults = {
            field_a = ""
        , field_b = ""
        ...
    }
</cfcomponent>

My problem is, I don't know how to define dynamic form fields inside this structure. The fields can have any 15-digit ID, so I need some kind of loop to preset the form fields, when I don't know the id and name of the field coming in.

Question:
How can I define form fields which use a dynamic 15-digit ID? If there is a better way to get the information into my CFC, I also wouldn't mind. I do have sellerID, buyerID, item-No and qty as record in my basket table, but when a user orders 10 items, I can't set 10 inputs with name ean/qty/buyer/seller in a form, can I? Nor can I param these values then inside my CFC. I'm lost.

2
why do u need to 'param' form fields? You can loop through form.fieldnames and treat fields that begin with 'qty' as one of the fields you pass into the CFC. What does your CFC need to do other than Defaults()? - Henry
My CFCs do a lot. I built them along this example, so I have a three layered CFC structure. Top layer controllers, 2nd layer services, third layer helpers. My service CFCs (e.g. shopping cart interactions) handle all the respective form processing, so every CFC has a default value list of all possible form inputs. This worked ok until the shopping cart, which uses records-ID to create inputs, which I can't "pre-param". But I found a solution. See below. - frequent
link missing.... bennadel.com/blog/… - frequent

2 Answers

1
votes

I can't totally answer your questions in a simple answer, but I can offer you some good advice that has served me well that might steer you a different and better direction.

Start using microdata. Create key/value pairs and use those instead of just the name property. Browsers won't render microdata. It is strictly used to hide relevant information. You can easily retrieve it using jQuery's data method or some way similar in raw JavaScript>

For exmaple, you can do something like this

<label data-dbrecord='' data-productid='' data-productqty=''></label>

Someone pointed me to this method a few months back and it has really helped. I think you'll see that I had similar issues using CFCs, Ajax, and JavaScript.

How to implement microdata attributes -- data-* and getting rid of the ID attribute?

0
votes

Ok. Problem was to find the right place to intercept the form and add all dynamic values.

In my case this was before form validation. My AJAX calls a method named "process", which passes the form values to server side validation, which I do here:

 <!--- VALIDATE --->
 <cffunction name="Validate" access="public" returntype="array" output="false" hint="validate form inputs">

      <cfscript>
           var LOCAL = {};
           var double = structNew();
           double.form = VARIABLES.Instance.FormData;
           double.criteria = VARIABLES.Instance.Validation;
      </cfscript>

      <cfinvoke component="fvalidate" method="val" double="#double#" returnvariable="valerrors"></cfinvoke>
      <cfset LOCAL.ErrorMessages = valerrors />
      <cfreturn LOCAL.ErrorMessages />
 </cffunction>  

So before invoke-ing my validation cfc, I added this:

 <cfloop collection="#VARIABLES.Instance.FormData#" item="formField">
      <cfscript>
      if ( LEFT(formField, 5) EQ "menge" OR LEFT(formField, 5) EQ "MENGE")
           VARIABLES.Instance.Validation[formField]="pass_or_criteria";
      </cfscript>
 </cfloop>

So now I'm looping over the form and am checking field names for "MENGE", which is my "val" from above example. If found, I add this field to my validation struct. This way it doesn't fail anymore and I don't to pre-define 100+ form inputs.

I guess I need to do the same when the form input defaults are needed, but this should work there, too.