Should I use Multiple RegularExpression attributes

后端 未结 1 950
庸人自扰
庸人自扰 2021-01-25 09:54

Update 8: The question has a new title ,hopefully it will help other avoid time consuming bugs...

I have the following code: (You need a reference t

1条回答
  •  醉话见心
    2021-01-25 10:23

    DO NOT extend RegularExpressionAttribute and set AllowMultiple to true
    It will bring you nothing but trouble.

    You can create 2 distinct attributes that inherit from RegularExpressionAttribute.

    public class MyModel
    {
        [MultipleRegExAttribute2(@"[^?.]{1,100}$")]
        [MultipleRegExAttribute3(@"^((?![><&])[\s\S])*$")]
        public string Name { get; set; }
    }
    
    
    public class MultipleRegExAttribute2 : RegularExpressionAttribute
    {
        public MultipleRegExAttribute2(string pattern) : base(pattern) { }
    }
    
    
    public class MultipleRegExAttribute3 : RegularExpressionAttribute
    {
        public MultipleRegExAttribute3(string pattern) : base(pattern) { }
    }
    

    Update

    A friend of mine showed me the root of the problem.
    I have to override the TypeId property.
    See this question:Custom validation attribute with multiple instances problem
    and this article :The Importance of TypeId in ASP.NET MVC DataAnnotations Validation Attributes

    0 讨论(0)
提交回复
热议问题