I had this discussion some time back on cfaussie. I created a form element builder cfc, which I use to build my forms dynamically and programmatically. Ie #application.elementbuilder.textBox(name='foo',value='hello')#; #application.elementbuilder.hidden(name='bar',value='byebye')#;
On the initial call to the builder, the html for the form element is passed back as a string (returned, not outputted) with its value set to the value parameter. On post back, I init a validation object, which I pass a copy of the form data, and a datatypes struct (datatypes.string = 'name,address'). I have validation for dates, numeric, etc. and it loops through the datatypes.string, datatypes.numeric values as a list, finds form key and validates the value If any errors are found, a structure containing the errors is returned and output to the user. Then I set a structure in the elementbuilder called tempform, which is a copy of the form data and is set in the variables scope, I also set an errors structure which is a copy of the errors struct returned from the validation obj. The form template now reloads, but the function which is called below checks to see if temp form is present in variables, and if it is, overrides the value with the value of the key in tempform, and data is re bound. #application.elementbuilder.textBox(name='foo',value='hello')#; If on repost data validation works, then I populate my bean and call its dao, and voila, This way I don't have redundant functions floating around in places they aren't needed, my data is re bound automatically and errors are outputted. Cheers Jamo -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Edward Chowdhury Sent: 28 January 2005 16:42 To: [email protected] Subject: RE: [CFCDev] Validating and Persisting Form Data I use a bean as a transfer object between the form validator and the dao. There is one form validator object per form. The form validators all inherit from a superclass object. The superclass object contains shared functionality such as zipcode validation routines, credit card mod 10 checks, required fields, error display etc. Each form validator object is responsible for setting up the form fields, and how each one should be validated and returns the transfer object with complete information if validation was successful or formHasErrors flag. A manager object is responsible for taking the form variables, instantiating the appropriate form validator object, passing in the form variables, getting the completely filled out transfer object out of the form validator and passing it into the dao object for entry into the database. Alternatively, in case there were errors, it loads in the form page containing the html for the form and passes it this form validator object. The page has calls to getError("fieldName") from the form validator object which will display the error or an empty string if there was no error for that particular field. ed -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Dawson, Michael Sent: Friday, January 28, 2005 8:55 PM To: [email protected] Subject: [CFCDev] Validating and Persisting Form Data This was previously discussed in the "Validation" thread back in November, but I never really "got it". This list has several new members so I would be interested in hearing new solutions, not that I discount the "elder's" opinions. ;-) I have a web-based form and I need to persist the data in a database. I also want to validate the data and, if there are errors, reload the form and display the errors to the user. (The form is self-posting to make it easier to re-populate the fields with just-submitted data.) I also have a simple bean that contains only getters and setters that equate to the form fields. The setters are all "typed" (e.g. String, Numberic, etc.) according to the requirements. (This is the data that needs to be validated, eventually, before saving to a database.) I have a DAO that Creates, Updates and Deletes a single record in the database. (This DAO does not validate any data. It only assumes the data passedto it has already been validated.) This is my current situation as mentioned above: TheForm -> Bean -> DAO -> Database If I need to validate the data from the form, where would the logic reside? Should I use: TheForm -> TheFormValidationObj -> Bean -> DAO -> Database Or, do I put the validation in one of the existing layers? Keep in mind that I will probably need to reuse some of the validation functions for other objects. If anyone could post a fully-working sample of this simple application, I'm sure many others would greatly appreciate it. I have a simple working application, but, being new to any OOP thinking, some things just don't "feel right" to me. Thanks M!ke ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by Mindtool, Corporation (www.mindtool.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by Mindtool, Corporation (www.mindtool.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by Mindtool, Corporation (www.mindtool.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
