简体   繁体   English

验证错误消息未清除

[英]Validation Error messages not clearing

I am using jquery validation & jquery.validate.unobtrusive on my application. 我在我的应用程序上使用jquery验证和jquery.validate.unobtrusive。

On my page I have about 4-5 controls. 在我的页面上,我有大约4-5个控件。 Out of these some are hidden. 其中有些是隐藏的。 There are 3 buttons on my page. 我的页面上有3个按钮。 On the click of the first button I am showing the hidden controls (radiobutton and dropdown). 在单击第一个按钮时,我将显示隐藏的控件(单选按钮和下拉列表)。 All of these are required fields. 所有这些都是必填字段。 When the user clicks the other button which is the submit button all the required fields show up just fine. 当用户单击另一个按钮(即“提交”按钮)时,所有必填字段都会很好地显示出来。 The third button is used to toggle between a different view which has its own controls. 第三个按钮用于在具有自己的控件的不同视图之间切换。

My issue is when the user submits the form using the second button and if the fields are not populated it shows the required field message fine. 我的问题是,当用户使用第二个按钮提交表单时,如果未填充字段,它将显示必填字段消息。 At this point when I click on 3rd button to toggle to a different view and then toggle back to my view the error messages are still there. 此时,当我单击第三个按钮以切换到其他视图,然后切换回我的视图时,错误消息仍然存在。 I am not able to clear out these error messages. 我无法清除这些错误消息。 See below radio button that I have: 请参阅下面的单选按钮:

<div class="col-sm-10">
    <label class="radio-inline">
       <input type="radio" value="myValue1" asp-for="Name" class="required" 
           data-bind="checked: name" />Name value 1
    </label>
<label class="radio-inline">
    <input type="radio" value="myValue2" asp-for="Name"  data-bind="checked: 
     name" />Name value 2
</label>
<span asp-validation-for="Name" class="danger"></span>

I am using knockout and on button click of the third button where I toggle I try to use the below code to clear out and reset the form but it does not work. 我正在使用敲除并在要切换的第三个按钮上单击按钮,我尝试使用以下代码清除并重置表单,但是它不起作用。

        var validator = $("form").validate();
        validator.resetForm();

Another thing is while submitting the form I am using the following code to force the validation as these controls are initially hidden: 另一件事是,在提交表单时,我正在使用以下代码来强制进行验证,因为这些控件最初是隐藏的:

        $("form").removeData("validator");
        $("form").removeData("unobtrusiveValidation");   
        $.validator.unobtrusive.parse($("form"));

Lastly I can see the following span that is generated in the html: 最后,我可以看到html中生成的以下范围:

<span class="help-block-msg field-validation-valid" data-valmsg-for="Name" 
data-valmsg-replace="true">
<span id="Name-error" class="">This field is required.</span>
</span>

You can use the following code to clear out 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();

This has been taken from: How to clear jquery validate errors 摘自: 如何清除jquery验证错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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