Solved

Implementing dynamic controls and validations in COLDFUSION

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now