Desvinculación del validador de jQuery

Estoy usando el Validador de jQuery. And I'm trying to figure out: How can I un-attach the validator from my form once its submitted?

$(function() {
  // Just the rules, etc...
  validator = $("#sweepstakesForm").validate({rules: {fname: {required: true,minlength: 2,maxlength: 50},lname: {required: true,minlength: 2,maxlength: 50},email: {email: true,remote: "?c=home&m=jsHook&param=email&nojson=1",required: true,minlength: 6,maxlength: 120},address: {required: true,minlength: 5,maxlength: 100},state_0: {required: true},state:  ); // etc ...

  $('#sweepstakesForm').submit() {

  }
});

I have tried these to no avail:

validator = null;
$("#sweepstakesForm").unbind('validate');
delete validator;

preguntado el 09 de marzo de 12 a las 23:03

Eché un vistazo rápido a la fuente and from what I can see, there is no method to un-attach/destroy the validator. -

Por qué exactamente are you trying to kill the validator? Once a form is validated and submitted, you're either on a new page, the submit button is disabled, or the form is disabled/hidden... killing the validator is a moot point. Perhaps if we knew more, we'd be able to suggest a workaround. -

one sure way would be reload whole form as new html -

4 Respuestas

Trata

$('#sweepstakesForm').data('validator', null);
$("#sweepstakesForm").unbind('validate');

respondido 11 mar '12, 21:03

The first one "works" for me too, but perhaps only because it gives an error, Uncaught TypeError: Cannot read property 'settings' of null line 332 of jquery.validate.1.11.1.js, if you re-enter invalid data after resetting the form. - tim peterson

Thanks, this worked for me. Also, considering this is a two year old answer it's worth pointing out that i've tested this and can confirm it works on jQuery 1.7.2. - crmpicco

Como un aparte, a partir de jQuery 1.7 off() se prefiere a unbind(). - Paul

I looked through the source code and it turns out there's a destroy() method that does this: this.resetForm(); $(this.currentForm).off(".validate").removeData("validator"); - Paul

Intente agregar un .unbind('submit')

$('#sweepstakesForm').submit(function () {
    $(this).unbind('submit');
});

respondido 09 mar '12, 23:03

That's not what he asked. He wants to kill the validator. - Sparky

@Sparky672, it's an interesting answer though. Presumably the validator has bound to submit events, and unbinding all submit handlers should effectively stop further validation. But I'm guessing it's a minefield of side-effects. - falta de párpados

Thank you for posting a comment, the unbind was not exactly was I was looking for, but I appreciate the comment! - JREAM

I'm not really familiar with this plugin, but from the documentation it looks like what you want is:

$('#sweepstakesForm').rules('remove');

respondido 10 mar '12, 00:03

Its not what i was looking for but ill keep this on the back burner, thank you! - JREAM

@JREAM, does it not prevent further validation? From my reading of the docs, it seems to do what you've asked for, maybe you can explain the difference. - falta de párpados

Hello from 2020 :) My validator version is still 1.11.1 and none of the code snippets above worked properly (without errors in console). The one that did is:

$('form,input,select,textarea,button').attr("novalidate", "novalidate").attr("formnovalidate", "formnovalidate")

I know it affects many elements on the page, but for debugging its fine IMHO.

Respondido 10 Abr '20, 14:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.