【问题标题】:Foolproof multiple validators on the same fields相同字段上的万无一失的多个验证器
【发布时间】:2015-02-13 16:25:52
【问题描述】:

我目前使用万无一失的验证:

[RequiredIfNot("type", 3, ErrorMessage = "Please enter at least one value")]
public int[] audites { get; set; }

但客户想要添加验证:

[RequiredIfNot("type", 3, ErrorMessage = "Please enter at least one value")]
[RequiredIfNot("nature", 1, ErrorMessage = "Please enter at least one value")]
[RequiredIfNot("nature", 3, ErrorMessage = "Please enter at least one value")]
public int[] audites { get; set; }

但它的编译器不同意这一点,那么如何将两者结合起来呢?

提前致谢!

【问题讨论】:

  • " it 编译器不同意那个" 是什么意思?它不编译吗?它在运行时不起作用吗?
  • 它不想编译

标签: asp.net asp.net-mvc asp.net-mvc-4 foolproof-validation


【解决方案1】:

Foolproof.RequiredIfNotAttribute 派生自 Foolproof.ModelAwareValidationAttribute(又派生自 System.ComponentModel.DataAnnotation.ValidationAttribute)。 ModelAwareValidationAttribute 标有[AttributeUsage(AttributeTargets.Property)]Refer source code。默认情况下,AttributeUsageAllowMultiple 参数为 false,这意味着您只能将属性应用于属性一次。您已尝试应用 3 次,因此出现错误。

拥有true 并允许它被多次应用可能会导致设置不显眼验证使用的$.validator.methods$.validator.unobtrusive.adapters 函数时出现问题。

您将需要使用其他一些验证属性或创建自己的实现IClientValidatableValidationAtribute,或依赖服务器端验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多