Add Comment
CFFORM with Custom onSubmit Tutorial
ColdFusion Tutorial #8
I've seen a couple of people trip over this so I thought I'd write something about the issue.
demo.cfm
If you use cfform and have your own onSubmit handler to perform custom field validation, you probably have something that looks like this:<cfform action="someAction.cfm" method="POST" name="myForm" onSubmit="return checkFields(this)">
If you have any auto-validation on your cfinput forms, ColdFusion will rewrite the onSubmit to call its auto-generated validation Javascript that will in turn call your original onSubmit code.
That fixes a bug in earlier versions where either your Javascript would run or the auto-generated Javascript would run, but not both. However, there is a subtle side-effect: the above code stops working.
Why is that? ColdFusion ends up calling checkFields(this) from inside the auto-generated Javascript and this refers to that code, not the original form. The fix is to use a named reference to the form object instead of this:
Demo
See this code running!
Download
Download this code as a zip!
Comments
Thanks, very useful tip. I have tripped over this myself, and usually just don't use cfform validation if I need anything custom.Michael Evangelista @ Wednesday 12 Sep 2007 - 12:46:53 AM
Since I upgrade to CF MX7 any auto-validation on my cfinput forms stops working,
On submit only my custom Javascript calls
Your example working OK on CF 6.1 but auto-validation does not work on CF MX7
Please advise
Thanks
Mikhail @ Saturday 12 Apr 2008 - 01:01:48 AM
Luckily for us there is validate function which rids the need for that.
Jody @ Thursday 15 Jan 2009 - 08:05:49 AM
Click button to add a comment
Author
Sean Corfield
Published
Tuesday 11 Sep 2007Original
This tutorial has been modified and published with permission of the author. The original tutorial can be found herehttp://corfield.org/blog/index.cfm/do/blog.entry/entry/cfform_and_onSubmit