Search code examples
c#asp.net-coremodel-validation

How can I create a "decorator abbreviation" in ASP.net Core


I am working on a project with a lot of models where I need the same validation with localized error messages happen at a couple of different places. Right now I decorate the corresponding properties with

        [RegularExpression(Bla, ErrorMessageResourceType = typeof(Blub), ErrorMessageResourceName = Blablablub)] 
        public int SomeInt { get; set; }

How can I specify a shorthand for this with a custom validation attribute? What I want is something like

        [MyRegularExpressionDecorator] 
        public int SomeInt { get; set; }

Any input is greatly appreciated. Thanks!


Solution

  • public class MyRegularExpressionDecoratorAttribute : RegularExpressionAttribute
    {
        public MyRegularExpressionDecoratorAttribute()
            : base("bla") 
        {
            ErrorMessageResourceType = typeof(Blub);
            ErrorMessageResourceName = "Blablablub";
        }
    }