asp.net mvc2 验证

发布于 2024-09-24 23:00:39 字数 211 浏览 2 评论 0原文

我正在使用 DataAnnotations 进行验证(包括客户端),

我有一个包含多个字段的表单。各个字段的基本验证工作正常。现在有几个字段,其中至少一个需要有值(如果有 3 个字段,则第一个、第二个或第三个字段应该有一个值)。

我在这个网站上阅读了很多帖子和一些博客文章。但我找不到适用于上述场景的解决方案。我可能错过了一些事情或者做得不正确。

你能帮忙吗?

I am using DataAnnotations for validation (including client side)

I have a form with multiple fields. Basic validation for individual fields work fine. Now there are a couple of fields of which atleast one needs to have a value (if there are 3 fields then either 1st or 2nd or 3rd field should have a value).

I have read quite a few posts on this site and couple of blog entries. But I couldn't find a solution that works in the above mentioned scenario. I might have missed something or doing it incorrectly.

Can you help with this please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2024-10-01 23:00:39

尝试这个

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EitherOr : ValidationAttribute
{
    private const string _defaultErrorMessage = "'{0}' OR '{1}' OR '{2}' must have a value";
    private readonly object _typeId = new object();

    public EitherOr(string prop1, string prop2, string prop3)
        : base(_defaultErrorMessage)
    {
        Prop1 = prop1;
        Prop2 = prop2;
        Prop3 = prop3;

    }

    public string Prop1 { get; private set; }
    public string Prop2 { get; private set; }
    public string Prop3 { get; private set; }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, Prop1, Prop2,Prop3);
    }

    public override bool IsValid(object value)
    {
        if(string.IsNullOrEmpty(Prop1)&&string.IsNullOrEmpty(Prop2) && string.IsNullOrEmpty(Prop3))
        {
            return false;
        }
        return true;
    }

,然后用 EitherOr 属性标记你的类:

[EitherOr("Bar","Stool","Hood", ErrorMessage = "please supply one of the properties")]
    public class Foo
    {
        public string Bar{ get; set;}
        public string Stool{ get; set;}
        public string Hood{ get; set;}
    }

请注意,我使用了字符串属性,如果你的属性是其他类型,请务必更改 IsValid(object value) 验证

try this

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EitherOr : ValidationAttribute
{
    private const string _defaultErrorMessage = "'{0}' OR '{1}' OR '{2}' must have a value";
    private readonly object _typeId = new object();

    public EitherOr(string prop1, string prop2, string prop3)
        : base(_defaultErrorMessage)
    {
        Prop1 = prop1;
        Prop2 = prop2;
        Prop3 = prop3;

    }

    public string Prop1 { get; private set; }
    public string Prop2 { get; private set; }
    public string Prop3 { get; private set; }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, Prop1, Prop2,Prop3);
    }

    public override bool IsValid(object value)
    {
        if(string.IsNullOrEmpty(Prop1)&&string.IsNullOrEmpty(Prop2) && string.IsNullOrEmpty(Prop3))
        {
            return false;
        }
        return true;
    }

then mark your class with the EitherOr attribute:

[EitherOr("Bar","Stool","Hood", ErrorMessage = "please supply one of the properties")]
    public class Foo
    {
        public string Bar{ get; set;}
        public string Stool{ get; set;}
        public string Hood{ get; set;}
    }

Please note that i made use of string properties, if your property is of other type, makle sure to change the IsValid(object value) validation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文