相同字段上的万无一失的多个验证器 [英] Foolproof multiple validators on the same fields

查看:93
本文介绍了相同字段上的万无一失的多个验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用万无一失的方式进行验证:

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.RequiredIfNotAttributeFoolproof.ModelAwareValidationAttribute派生(又从System.ComponentModel.DataAnnotation.ValidationAttribute派生). ModelAwareValidationAttribute[AttributeUsage(AttributeTargets.Property)]标记. 请参阅源代码.默认情况下,AttributeUsageAllowMultiple参数是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.

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

You will need to use some other validation attributes or create your own ValidationAtribute that implements IClientValidatable, or rely on server side validation.

这篇关于相同字段上的万无一失的多个验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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