【问题标题】:ASP.NET MVC. How disable required validation based on a parameter?ASP.NET MVC。如何根据参数禁用所需的验证?
【发布时间】:2016-07-29 15:24:37
【问题描述】:

我确实有一个实体类,其中包含一些必需的属性,具体取决于选择器。

例如:选择器可以假定为“1”或“2”。如果选择器为“1”,则需要一组参数。如果选择器为“2”,则需要另一组参数。

class MyClass{

    public int Selector {get;set;} // 1 or 2

    public string A_required_for_1 {get;set;}
    public string B_required_for_1 {get;set;}

    public string C_required_for_2 {get;set;}
    public string D_required_for_2 {get;set;}

    public string E_Required_for_both_selectors {get;set;}

}

用户应该能够在视图中的创建或编辑操作期间在选择器之间切换。

客户端验证已经解决。

在服务器验证中如何处理?

【问题讨论】:

  • 它不是“在某些情况下禁用必需的验证属性”重复
  • 帮自己一个忙,在您的应用程序中分离 EDIT 和 CREATE 功能。
  • @NicholasKinney 我不明白为什么你的评论适用于这个问题。你能澄清一下吗?
  • 如果您使用强类型对象强烈键入视图,那么您将希望在 EDIT 和 CREATE 视图之间分离对象。我希望这会有所帮助。
  • 希望link 可以解决您的问题。

标签: c# asp.net asp.net-mvc validation requiredfieldvalidator


【解决方案1】:

您可以创建自己的自定义验证属性或使用MVC Foolproof Validation,然后执行以下操作:

class MyClass
{

    public int Selector {get;set;} // 1 or 2

    [RequiredIf("Selector == 1", ErrorMessage = "Your Error Message")]
    public string A_required_for_1 {get;set;}

    [RequiredIf("Selector == 1", ErrorMessage = "Your Error Message")]
    public string B_required_for_1 {get;set;}

    [RequiredIf("Selector == 2", ErrorMessage = "Your Error Message")]
    public string C_required_for_2 {get;set;}

    [RequiredIf("Selector == 2", ErrorMessage = "Your Error Message")]
    public string D_required_for_2 {get;set;}

    [Required("Your Error Message")]
    public string E_Required_for_both_selectors {get;set;}

 }

正如 Win 所提到的,它似乎已经有一段时间没有积极开发了,所以您可能想要沿着创建自己的自定义验证属性的路线走下去,这确实需要更多的工作,但您可以更好地控制验证本身。根据您的需要选择。

对于自定义验证属性,您可以执行以下操作:

public class RequiredIfOtherProperty : ValidationAttribute
{
    private readonly string _otherPropertyName;
    private readonly string _compareValue;

    public RequiredIfOtherProperty(string otherPropertyName, string compareValue)
    {
        _otherPropertyName = otherPropertyName;
        _compareValue = compareValue;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var otherProperty = validationContext.ObjectType.GetProperty(_otherPropertyName);
        if (otherProperty == null)
        {
            return new ValidationResult($"Property '{_otherPropertyName}' does not exist");
        );

        var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);
        if (!_compareValue.Equals(otherPropertyValue))
        {
            return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
        }

        return null;
    }
}

它应该让您大致了解可以做什么,并且您可以将实际验证更改为您喜欢的任何方式。然后您可以像使用普通属性一样使用它,例如

[RequiredIfOtherProperty("SomeProperty", "ValueToCompareWith")]

【讨论】:

  • 不错的库,但它是 Beta 0.9.4517 并且自 2012 年 5 月 14 日以来没有更新。
  • “自定义验证属性”是什么意思?
【解决方案2】:

我相信 mvcfoolproof 将适用于这种情况 [https://foolproof.codeplex.com/][1] 它也可以在 nuget 上使用。它添加了额外的验证属性,例如

[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]

使用起来非常简单。

【讨论】:

  • 不错的库,但它是 Beta 0.9.4517 并且自 2012 年 5 月 14 日以来没有更新。
猜你喜欢
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 2011-08-28
相关资源
最近更新 更多