相同字段上的万无一失的多个验证器 [英] Foolproof multiple validators on the same fields
问题描述
我目前使用万无一失的方式进行验证:
I currently use foolproof for that validation:
[RequiredIfNot("type", 3, ErrorMessage = "Please enter at least one value")]
public int[] audites { get; set; }
但是客户希望添加验证:
but customer wants to add validation as that:
[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; }
但是它的编译器不同意,那么In如何将两者结合起来?
But it compilator disagree with that, so how can In combine both?
提前谢谢!
推荐答案
Foolproof.RequiredIfNotAttribute
从Foolproof.ModelAwareValidationAttribute
派生(又从System.ComponentModel.DataAnnotation.ValidationAttribute
派生). ModelAwareValidationAttribute
用[AttributeUsage(AttributeTargets.Property)]
标记. 请参阅源代码.默认情况下,AttributeUsage
的AllowMultiple
参数是false
,这意味着您只能将属性应用一次到属性.您尝试将其应用了3次,因此出现了错误.
The Foolproof.RequiredIfNotAttribute
derives from Foolproof.ModelAwareValidationAttribute
(which in turn derives from System.ComponentModel.DataAnnotation.ValidationAttribute
). ModelAwareValidationAttribute
is marked with [AttributeUsage(AttributeTargets.Property)]
. Refer source code. By default the the AllowMultiple
parameter of AttributeUsage
is false
which means that you can only apply the attribute once to a property. You have tried to apply it 3 times, hence the error.
具有true
并允许其多次应用可能会导致设置用于非侵入式验证的$.validator.methods
和$.validator.unobtrusive.adapters
函数时出现问题.
Having it true
and allowing it to be applied multiple times would possibly cause problems in setting the $.validator.methods
and $.validator.unobtrusive.adapters
functions used by unobtrusive validation.
您将需要使用其他一些验证属性,或者创建自己的实现IClientValidatable
的ValidationAtribute
,或者依赖服务器端验证.
You will need to use some other validation attributes or create your own ValidationAtribute
that implements IClientValidatable
, or rely on server side validation.
这篇关于相同字段上的万无一失的多个验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!