从submitHandler函数中获取jQuery验证器对象

时间:2021-12-31 20:37:18

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

我试图从submitHandler函数中调用jQuery验证showErrors函数,而不必像这样保存验证器对象:

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:

有没有办法从submitHandler方法中获取验证器对象,如下所示:

$( "#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.

通过这种方式,我希望我可以在整个项目的多个地方使用相同的submitHandler方法。

1 个解决方案

#1


19  

Validator object is stored in form element data:

Validator对象存储在表单元素数据中:

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);
      }
    });

  }
});

#1


19  

Validator object is stored in form element data:

Validator对象存储在表单元素数据中:

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);
      }
    });

  }
});