Any way to selectively disable ColdFusion's built-in form validation?

I am working with some old code. It uses the built-in form validation ColdFusion (i.e. _requiredhidden fields). I want to add a cancel button to the form. The Cancel button should actually handle some business logic (therefore, I cannot just set its location.href to another page). The problem is that since the cancel button is a submit button, it starts the built-in check and the user receives the error message that is required for this field.

Is there a way to disable validation for this submit button? I would prefer not to try to change the base code that forms the form, as it is used in several places. Here is a very 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>

, , - onclick "" "_required" DOM. , . Javascript, :

<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>
+5
1

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

+5

All Articles