Solved

Implementing dynamic controls and validations in COLDFUSION

Posted on 2010-09-17
5
322 Views
Last Modified: 2013-12-24
Hi,

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.

   Example
   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.
0
Comment
Question by:Cashmgmt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:reiters
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.
0
 

Author Comment

by:Cashmgmt
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.
0
 
LVL 6

Expert Comment

by:reiters
ID: 33704848
You can do things like.

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

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.
0
 

Accepted Solution

by:
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.

0
 
LVL 6

Expert Comment

by:reiters
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.
0

Featured Post

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

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

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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