Foolproof multiple validators on the same fields

亡梦爱人 提交于 2019-12-02 22:20:33

问题


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; }

But it compilator disagree with that, so how can In combine both?

Thanks in advance!


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/28504122/foolproof-multiple-validators-on-the-same-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!