Solved

Implementing dynamic controls and validations in COLDFUSION

Posted on 2010-09-17
5
317 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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