删除mvc中的客户端验证错误

时间:2018-03-22 15:34:27

标签: javascript jquery asp.net-mvc unobtrusive-validation

我有一个mvc表单,用户可以在其中添加动态元素。我正在使用MVC进行不引人注意的验证。 我还有两个按钮,一个保存为草稿,另一个提交。提交按钮正确触发验证,包括添加的动态元素。

但是,如果单击“保存为草稿”,我将很难在客户端尝试清除验证属性data-val- *。

无论我做什么,我都无法触发表单提交以调用我的操作方法。显然只有这样我才能处理禁用服务器端验证。

我尝试了以下内容

                 $("#myform").submit(function () {
                    ---
                    //some code to check which submit button was clicked
                    //if save as draft was clicked, proceed below
                    ---
                    var settings = $("#myform").validate().settings;
                    for (var ruleIndex in settings.rules) {
                        delete settings.rules[ruleIndex];

                    $('#myform').clearErrors();

                    //$("#myform").removeData("validator")    
                    //    .removeData("unobtrusiveValidation");
                    $("select").removeAttr("data-val-required");
                    $("select").removeAttr("data-val-number");

                    $("input").removeAttr("data-val-required");


                    if ($("#myform").valid()) {
                        alert("valid");  //I can reach here indicating that the form is valid at this point.But still my action  method is not called.
                    }
                }

             $.fn.clearErrors = function () {

                $(this).each(function () {

                    $(this).find(".field-validation-error").empty();
                    $(this).find(".input-validation-error").removeClass("input-validation-error");
                    $(this).trigger('reset.unobtrusiveValidation');
                });
            };

$("#myform").submit(function () { --- //some code to check which submit button was clicked //if save as draft was clicked, proceed below --- var settings = $("#myform").validate().settings; for (var ruleIndex in settings.rules) { delete settings.rules[ruleIndex]; $('#myform').clearErrors(); //$("#myform").removeData("validator") // .removeData("unobtrusiveValidation"); $("select").removeAttr("data-val-required"); $("select").removeAttr("data-val-number"); $("input").removeAttr("data-val-required"); if ($("#myform").valid()) { alert("valid"); //I can reach here indicating that the form is valid at this point.But still my action method is not called. } } $.fn.clearErrors = function () { $(this).each(function () { $(this).find(".field-validation-error").empty(); $(this).find(".input-validation-error").removeClass("input-validation-error"); $(this).trigger('reset.unobtrusiveValidation'); }); };

1 个答案:

答案 0 :(得分:0)

尝试在"上添加formnovalidate属性并另存为#34;按钮如中所述 Documentation for jqueryvalidation

示例:

<input type="submit" formnovalidate name="saveAsDraft" value="Cancel">

您可能必须使用class =&#34;取消&#34;如果您使用的是旧版本,则使用attribute而不是formnovalidate属性。 示例:

<input type="submit" class="cancel" name="saveAsDraft" value="Cancel">