Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgreen/5669244 to your computer and use it in GitHub Desktop.
Save danielgreen/5669244 to your computer and use it in GitHub Desktop.
This is an amended fragment of jquery.validate.js (https://github.com/jzaefferer/jquery-validation). The change is to take the value returned from validator.settings.submitHandler.call, and return that value from the handle function. This means that a page that uses submitHandler to take some action upon a successful validation can simply return…
// validate the form on submit
this.submit( function( event ) {
if ( validator.settings.debug ) {
// prevent form submit to be able to see console output
event.preventDefault();
}
function handle() {
var hidden;
if ( validator.settings.submitHandler ) {
if (validator.submitButton) {
// insert a hidden input as a replacement for the missing submit button
hidden = $("<input type='hidden'/>").attr("name", validator.submitButton.name).val(validator.submitButton.value).appendTo(validator.currentForm);
}
var doSubmit = validator.settings.submitHandler.call( validator, validator.currentForm, event );
if (validator.submitButton) {
// and clean up afterwards; thanks to no-block-scope, hidden can be referenced
hidden.remove();
}
// Return the result of submitHandler instead of always returning false
// Means that the presence of submitHandler does not have to block propagation of the submit event
return doSubmit;
}
return true;
}
// prevent submit for invalid forms or custom submit handlers
if ( validator.cancelSubmit ) {
validator.cancelSubmit = false;
return handle();
}
if ( validator.form() ) {
if ( validator.pendingRequest ) {
validator.formSubmitted = true;
return false;
}
return handle();
} else {
validator.focusInvalid();
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment