I am working with some old code. It uses built-in ColdFusion form validation (i.e. _required
hidden fields). I want to add a cancel button to the form. The cancel button has to actually handle some business logic (so I can't just make it set location.href to some other page). Problem is that, because the cancel button is a submit button, it triggers that built-in validation and the user gets an error that the field is required.
Is there any way to disable the validation for that particular submit button? I'd rather not try to modify the underlying code which builds the form, as it is used in a bunch of places. Here is a greatly simplified version of my code:
<cfif IsDefined("Form.OK")>
You clicked OK!
<cfelseif IsDefined("Form.Cancel")>
You clicked Cancel!
</cfif>
<cfoutput>
<form action="#CGI.Path_Info#" method="POST">
Enter Name: <input type="text" name="Name" value="" /><br/>
<input type="hidden" name="Name_required" value="" />
<input type="submit" name="OK" value="OK" />
<input type="submit" name="Cancel" value="Cancel" />
</form>
</cfoutput>
One thing that I thought of is to make the onclick of the Cancel button remove any hidden "_required" fields from the DOM. This works, but it feels very hacky. Here's the Javascript I used for that approach:
<script type="text/javascript">
function removeRequiredFields() {
var els = document.getElementsByTagName('input');
for(var i = 0; i <= els.length; i++) {
if(els[i].type == 'hidden' && els[i].name.endsWith('_required'))
els[i].parentNode.removeChild(els[i]);
}
}
</script>
CF9+ - Application.cfc
this.serverSideFormValidation="false";
http://www.raymondcamden.com/index.cfm/2009/7/12/My-first-ColdFusion-9-scoop--disable-server-side-validation
CF8 - Application.cfc
<cfset this.name = "myApplication">
<cfset url.form = structnew()/>
<cfset structappend(url.form,form)/>
<cfset structclear(form)/>
<cffunction name="onRequestStart">
<cfset structappend(form,url.form)/>
<cfset structdelete(url,"form")/>
</cffunction>
http://www.cfinsider.com/index.cfm/2008/9/30/Getting-Around-ColdFusion-Form-Validation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With