简体   繁体   中英

Manual jQuery validation call after AJAX call not working?

I have a mixed client-side/server-side validation using jQuery validation plugin. The validation happens on both submit and for some fields on change. The form is being submitted via AJAX. There is another validation going on the application just before updating the DB. If the data changes are not stored to the database due to this validation failing I'm returning the result via JSON to the JS method handling the AJAX form submission. Ideally I would raise an error with custom message passed from the backend to JS by something like $.validator.showErrors(obj); as discussed here Unfortunately the validator.showErrors(...) method is not defined in that context:

$(document).ready(function() {
    $('.form').each(function() {
    $(this).submit(function(e) {
        e.preventDefault();
        if ($.submitForm()) {
    (...)
            $.post(url, $.param(context), function(data) {
                if (!data.success) {
                    $.validator.showErrors(...);
                    //$("#basicdetails").validate();
                }
            }, "json");
        }
        (...)

You can also see that I've tried form revalidation which should also trigger appropriate error (although more of an hack that the real solution).

I would like to retain static client-side validation and after ajax submission to be able to raise any errors that might have occurred on the backend.

Thank you for any help you can provide.

Pawel

.validate()用于设置验证,要触发验证,您应使用.valid()如下所示:

$("#basicdetails").valid();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM