0

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.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
frequent
  • 27,643
  • 59
  • 181
  • 333
  • 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 Jul 16 '12 at 21:39
  • My CFCs do a lot. I built them along this [example](http://www.bennadel.com/blog/1557-Object-Oriented-Form-Helpers-And-Reusing-Form-Validation-On-The-Client.htm), 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 Jul 16 '12 at 22:37
  • link missing.... http://www.bennadel.com/blog/1557-Object-Oriented-Form-Helpers-And-Reusing-Form-Validation-On-The-Client.htm – frequent Jul 16 '12 at 22:40

2 Answers2

1

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?

Community
  • 1
  • 1
Evik James
  • 10,335
  • 18
  • 71
  • 122
  • I'm already using data-"a-lot", but in this case I'm stuck,.. I was stuck. Posting my solution just now. – frequent Jul 16 '12 at 22:25
0

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.

frequent
  • 27,643
  • 59
  • 181
  • 333
  • As an aside, the two parts of your IF statement above mean the same thing. ColdFusion is not case sensitive when doing string comparisons, so having it twice is just cluttering up your code. All those switches in and out of cfscript are okay, but may lead any other developer who has to manage your code in the future to require therapy. I'd consider sticking to one style or the other within a given block of code wherever possible to prevent a future shortage of qualified mental health professionals. Not being mean, just a tip which will save you some grief later. – Justin Scott Jul 17 '12 at 03:34
  • @JustinScott - well taken. Just don't know how to do some things in CFscript (yet...) – frequent Jul 17 '12 at 04:47
  • as an aside. Do you know a good resource explaining whats available in CFscript and what is not (using CF8). I would rather do loops/functions and alike in cfscript, if I had a good source to get started – frequent Jul 17 '12 at 05:03
  • Unfortunately Adobe's documentation is a little weak in that area: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=CFScript_03.html – Justin Scott Jul 17 '12 at 14:38