Manually adding & removing validation errors to jQuery validator

前端 未结 3 1654
北恋
北恋 2021-02-02 06:46

I have a search form & knockout-based grid for results. When search is performed, there is some server-side validation taking place on asp.net mvc, and if model state is not

3条回答
  •  太阳男子
    2021-02-02 07:51

    I ended up solving the problem in a simple way cleaning the errors and then showing the new errors.

    // get the form inside we are working - change selector to your form as needed
    var $form = $("form");
    
    // get validator object
    var $validator = $form.validate();
    
    // get errors that were created using jQuery.validate.unobtrusive
    var $errors = $form.find(".field-validation-error span");
    
    // trick unobtrusive to think the elements were succesfully validated
    // this removes the validation messages
    $errors.each(function(){ $validator.settings.success($(this)); })
    
    // clear errors from validation
    $validator.resetForm();
    
    // Then show the errors
    $.validator().showErrors({prop:error}
    

提交回复
热议问题