使用ASP.NET Core删除所需的验证 [英] Remove required validation using asp.net core

查看:214
本文介绍了使用ASP.NET Core删除所需的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在表单上有两个按钮的客户端.一种可以保存未完成表格的进度的程序.因此,此表单仍将需要验证字段,但只会忽略所需的验证.另一个按钮将需要运行包含必填字段的完整验证.

I have a client that requires two buttons on the form. One that saves the progress of an uncompleted form. So this form will still need to validate the fields but will just ignore the required validation. The other button will need to run the complete validation included required fields.

我正在使用标准的asp.net核心项目,我相信它使用jquery-validation-unobtrusive并且基于视图模型.

I am using the stock standard asp.net core project which I believe uses jquery-validation-unobtrusive and is based on the model for the view.

我要做的是单击保存进度"按钮以删除所有必需的验证并提交表单.但是,在堆栈溢出或互联网上找不到的所有代码都无法满足我的情况.

What I am trying to do is when the "Save Progress" button is clicked to remove all the required validation and submit the form. However none of the code that I have found on stack overflow or the internet works for my scenario.

我尝试过:

<div class="form-group">
            <input id="Progress" type="submit" value="Save Progress" onclick="foo()" class="btn btn-default" />
        </div>

 function foo() {

        $('#FamilyName').rules("remove", "required");


        return true
    }

     function foo() {
        var settings = $('#Registration').validate().settings;
        for (var i in settings.rules) {
            delete settings.rules[i].required;
        }
 }

以上两次尝试均成功找到并删除了规则,但是在提交表单时,对于相关字段的验证仍然失败.

both of the above two attempts successfully find and remove rules but when the form is submitted validation still fails for the field in question.

我很困惑,不确定如何进行.

I am stumped and not sure how to proceed.

推荐答案

在客户端上更改required规则不会影响服务器端验证.您的[Required]属性验证仍在模型绑定过程中执行,因此ModelState将无效,并且您将需要手动检查每个属性以确定其是否由于[Required]属性或以下原因而无效:您的其他验证属性.

Changing the required rule(s) on the client does not affect server side validation. Your [Required] attribute validation is still executed during the model binding process, therefore ModelState will be invalid, and you would manually need to check each property to determine if its invalid because of a [Required] attribute, or because of one of your other validation attributes.

此外,您将需要重新添加所有规则(或重新解析$.validator),以对常规提交进行客户端验证.

In addition, you will need to add all the rules back again (or re-parse the $.validator) to get the client side validation for your normal submit.

相反,在视图模型中包含bool属性,以便可以使用条件[RequiredIf]属性(可以使用万无一失属性,或此项目中的一个)

Instead, include a bool property in your view model, so that you can use a conditional [RequiredIf] attribute (you could use a foolproof attribute, or the one in this project).

您的模型将包括

public bool IsDraft { get; set; }
[RequiredIf("IsDraft", false, ErrorMessage = "...")]
public string FamilyName { get; set; }

并在视图中

@Html.HiddenFor(m => m.IsDraft)

@Html.LabelFor(m => m.FamilyName)
@Html.TextBoxFor(m => m.FamilyName)
@Html.ValidationMessageFor(m => m.FamilyName)


<input id="Progress" type="button" value="Save Progress" class="btn btn-default" />
<input id="Final" type="button" value="Save Final" class="btn btn-default" />

和相关的脚本

$('#Progress').click(function() {
    $('#IsDraft').val('True');
    $(yourForm).submit();
});
$('#Final').click(function() {
    $('#IsDraft').val('False');
    $(yourForm).submit();
});

这篇关于使用ASP.NET Core删除所需的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆