Open accordion panel on validation error

前端 未结 3 1600
迷失自我
迷失自我 2021-02-10 09:38

I am using jQuery accordion to split my forms into several panels and jQquery validation to check the required fields. It works great to show errors in validated fields as long

3条回答
  •  Happy的楠姐
    2021-02-10 10:21

    For those who are using MVC, the invalidHandler is created for you. When I tried overriding it (from https://github.com/jzaefferer/jquery-validation/issues/765)

    $form.off('invalid-form.validate')
                    .on('invalid-form.validate', newInvalidHandler);
    

    The form would always submit.

    My solution is to pull out the function and call it when the submit is clicked. I also had to pull out the ignore=[]

    function ShowPanel() {
        var validator = $("form").validate();
        if (validator.numberOfInvalids() > 0) {
            validator.showErrors();
            var index = $(".input-validation-error")
                        .closest(".ui-accordion-content")
                        .index(".ui-accordion-content");
            $("#accordion").accordion("option", "active", index);
        } 
    }
    
    $("#Save").click(function () { if (!$("form").valid()) { ShowPanel(); } });
    
    $("form").data("validator").settings.ignore = [];
    

提交回复
热议问题