Implementing dynamic controls and validations in COLDFUSION

Posted on 2010-09-17
Last Modified: 2013-12-24

This is the scenario I have.  I have in my database , in a table, list of fields (like Name, Pin, address) and the validations to be performed on those fields (like limiting the salary to 2 decimal places etc).
These fields are not known at compile time and they are only known at runtime depending on say which user logs in. So for example user A will have fields like Name, address, occupation etc and User B may have fields like CompanyName, Number of Employess etc. Each of these fields have certain set of validations to be performed. For example a fields which is salary, I need to limit the user to only 2 decimal places. These validations need to be done at runtime and not when we submit a page.

This what I have done so far.

1. I make a call to the database to get the fields from the database and put it in a array. each field is stored in a structure. so finally we have an array of structures.

   dataEntryFields = ArrayNew(1);
           field1 = StructNew();
           field1.Name = "Amount";
           field1.FieldDataType = "A";
           field1.Length = 11;
           field1.Justification = "R";
           field1.MinAmount = 0.01;
           field1.MaxAmount = 100.00;
           field1.Message = "Enter Info for Check Amount";
           field1.Decimal = 2;
           AddIt = ArrayAppend(dataEntryFields,field1);
           field2 = StructNew();
           field2.Name = "INVNUM";
           field2.Prompt = "INVOICE NUMBER :";
           field2.FieldDataType = "C";
           field2.Length = 7;
           field2.Justification = "L";
           field2.Message = "ALPHA / NUMERIC; DEROG TEST (USE ALL 9'S AS DEFAULT)";
           field2.Decimal = 0;
           AddIt = ArrayAppend(dataEntryFields,field2);
   I am populating these fields on the coldfusion page by looping through the array. But I dont know how to validate these fields on the fly. I was thinking of calling a javascript function on the "ONChange" event of CfInput control, but how do you pass the name of the control to the javascript function?  Usually when the controls name is fixed, in the javascript function you can get access to the control by document.GetElementById function. Since these controls are dynamically populated from an array , I need a way to pass the controls name and also the array of fields to the javascript function.
That is just one part of the problem. After submitting the page, I need to perform validations on the form values. Since the form fields are dynamic, I need to pass to the response page an array containing the field name and field values. so an array of  structure  where structure has 2 fields (Name & Value). So what should I do to pass this array from the form page to the submit Page?

I am on coldfusion 9.
Question by:Cashmgmt
  • 3
  • 2

Expert Comment

ID: 33702473
I'm not completely following you but let me take a stab in the dark.  You could use jQuery to monitor the fields and ONBLUR fire a ajax call to a CFC or CFM.  You would pass an ID of some sort to the other page that tells it which validation is required and also pass the user's input.  On the other page you would look up the validation by name and apply it to the user's input and return a pass/fail status.

As for passing a structure in the for try this:
<cfwddx action="cfml2wddx" input="#my_struct#" output="WddxTextVariable">
<input type="hidden" name="struct_data" value="#WddxTextVariable#">

on the receiving page you would:
<cfwddx action="wddx2cfml" input="#form.struct_data#" output="my_struct">

If you need any additional assistance on the jQuery just let me know and i'll write something up.

Author Comment

ID: 33704216
I am not able to pass values from the cold fusion to the java script function. I think the java script function does not know about the cold fusion value because the alert statement inside the java script function always displays "[object]" though in the source code of the browser the cold fusion "OnBlur" event is calling the java script function with the right value.

Expert Comment

ID: 33704848
You can do things like.

<script type="text/javascript">
foo = #bar#;

That kinda over-simplified but hopefully get the idea.  I can't quite determine what you are looking for (im kinda thick).  Rest assure their is a way, just a matter of finding it.

If you are trying to pass a structure to JS then you may want to look at JSON.  Newer CF has JSON built in and older can be done with addon scripts.

Accepted Solution

Cashmgmt earned 0 total points
ID: 33717310
Finally figured out the solution. When I pass variables from coldfusion to Javascript I had to include the variables within single quotes.
I mean this worked :
<cfset controlName  = fields[fldNum].Name>
<td><cfinput type = "text" name = "#fields[fldNum].Name#" required = "yes" maxLength=#fields[fldNum].Length# OnBlur = "javascript:doValidation('#controlName#');"></td>

but this dint work:
<cfset controlName  = fields[fldNum].Name>
<td><cfinput type = "text" name = "#fields[fldNum].Name#" required = "yes" maxLength=#fields[fldNum].Length# OnBlur = "javascript:doValidation(#controlName#);"></td>

Something silly but took me a lot of time to figure out.


Expert Comment

ID: 33717566
I see.  So you were putting the result of a CF variable in the function as a parameter that JS was seeing as the name of a JS variable but that variable didn't exist in JS.  By placing the quotes, you were telling JS here is the actual value I want to use.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question