Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get jQuery validator object from within the submitHandler function

I am attempting to call the jQuery validation showErrors function from within the submitHandler function without having to saving the validator object like so:

var validator = $( "#myshowErrors" ).validate();
validator.showErrors({
    "firstname": "I know that your firstname is Pete, Pete!"
});

Is there a way to get the get the validator object from within the submitHandler method somewhat like this:

$( "#myshowErrors" ).validate({
  submitHandler : function(form) {

    $(form).ajaxSubmit({
      success : function(result) {

        var validator = // Get validator here somehow?
        validator.showErrors(result.fieldErrors);

      }
    });

  }
});

By doing it this way, my hope is that I can use the same submitHandler method in multiple places throughout my project.

like image 607
Vincent Catalano Avatar asked Sep 04 '13 19:09

Vincent Catalano


1 Answers

Validator object is stored in form element data:

var validator = $.data( form, "validator");

Edit:

In fact validator should be also stored in this:

$( "#myshowErrors" ).validate({
  submitHandler : function(form) {
    var validator = this;

    $(form).ajaxSubmit({
      success : function(result) {
        validator.showErrors(result.fieldErrors);
      }
    });

  }
});
like image 183
Krzysiek Avatar answered Oct 10 '22 21:10

Krzysiek