How to clear jquery validate errors

后端 未结 6 1031
灰色年华
灰色年华 2020-12-14 10:26

I\'m hijaxing an existing form and POSTing to the server. jQuery validate does most of the validation but if validation fails on the server we return the errors to the clien

相关标签:
6条回答
  • 2020-12-14 11:09

    May be I am wrong to clear the errors like this:

    function clearError(form) {
        $(form + ' .validation-summary-errors').each(function () {
            $(this).html("<ul><li style='display:none'></li></ul>");
        })
        $(form + ' .validation-summary-errors').addClass('validation-summary-valid');
        $(form + ' .validation-summary-errors').removeClass('validation-summary-errors');
        $(form).removeData("validator");
        $(form).removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse($(form));
    };
    

    I tried answer given in the comment by AaronLS but not got the solution so I just do it like this.

    Maybe helpful to someone.

    0 讨论(0)
  • $("#form").find('.field-validation-error span').html('')
    
    0 讨论(0)
  • 2020-12-14 11:16

    In .NET Core I have the form inside a builtin Bootstrap modal.

    For now I'm manually removing the error message spans from their containers, once the modal is starting to show, by using the additional .text-danger class of the error message container like so:

    $('#my-form').find('.text-danger').empty();
    

    so that I don't rely on container .field-validation-error that might have been already toggled to .field-validation-valid.

    The min.js versions of the libraries jquery.validate and jquery.validate.unobtrusive are loaded via the partial view _ValidateScriptsPartial.cshtml, so I played with them to see what resetForm() / valid() and native html form reset() do.

    So in my case $('#my-form').data("validator").resetForm() only resets some validator internals, not the form and it doesn't trigger the onReset() function in the unobtrusive library. The $('#my-form').valid() indeed removes the errors in the modal, but only if the modal is fully shown and valid. The native html form reset() is the only one that triggers both onReset() of unobtrusive library, and then the resetForm() of the validator. So it seems like we need to trigger the native html form document.querySelector('#my-form').reset() to activate the reset functions of both libraries/plugins.

    The interesting thing is that the unobtrusive library runs the simple jQuery empty() on the .field-validation-error class (the container of the error span message) only in its onSuccess() function, and not onReset(). This is probably why valid() is able to remove error messages. The unobtrusive onReset() looks like it's responsible only for toggling .field-validation-error class to .field-validation-valid. Hense we are left with a <span id="___-error">The error message</span> inside the container <span class="text-danger field-validation-error">...</span>.

    0 讨论(0)
  • 2020-12-14 11:25

    Here's the code I ended up using to clear/reset all errors. It's possible there's some redundancy in there, but it's working for me.

    function removeValidationErrors(frmId) {
        var myform = $('#' + frmId);
        myform.get(0).reset();
        var myValidator = myform.validate();
        $(myform).removeData('validator');
        $(myform).removeData('unobtrusiveValidation');
        $.validator.unobtrusive.parse(myform);
        myValidator.resetForm();
        $('#' + frmId + ' input, select').removeClass('input-validation-error');
    }
    
    0 讨论(0)
  • 2020-12-14 11:28

    You posted this a while ago, I don't know if you managed to solve it? I had the same problem with jQuery validate and the jQuery.validate.unobtrusive plugin.

    After examining the source code and some debugging, I came to the conclusion that the problem comes from the way the unobtrusive plugin handles error messages. It removes the errorClass that the jQuery.validate plugin sets, and so when the form is reset, jQuery validate cannot find the error labels to remove.

    I did not want to modify the code of the plugins, so I was able to overcome this in the following way:

    // 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();
    

    note: I use the $ prefix for variables to denote variables that contain jQuery objects.

    0 讨论(0)
  • 2020-12-14 11:28

    The reason this is still an issue (even 6 years on) is that jQuery Validation doesn't have an event handler for when your form is valid; only for when it's invalid.

    Unobtrusive Validation taps into the Invalid handler to add your errors to your Validation Summary elements. (Specifically, any element with data-valmsg-summary=true.) But because there's no Valid handler, there's no way for Unobtrusive Validation to know when they can be cleared.

    However, jQuery Validation does allow you to supply your own showErrors method, which is called after every validation check, whether the result is valid or invalid. Thus, you can write a custom function that will clear those validation summary boxes if your form is valid.

    Here's a sample that will apply it globally. (You could apply it to specific instances of your validators by using settings, but since I always want this functionality, I just put it in the defaults object.)

    $.validator.defaults.showErrors = function () {
        if (!this.errorList.length) {
            var container = $(this.currentForm).find("[data-valmsg-summary=true]");
            container.find("ul").empty();
            container.addClass("validation-summary-valid").removeClass("validation-summary-errors");
        }
    
        // Call jQuery Validation's default showErrors method.
        this.defaultShowErrors();
    };
    

    This also has the benefit of clearing the validation summary box the moment your form is valid, instead of having to wait for the user to request a form submission.

    0 讨论(0)
提交回复
热议问题