RegularExpression Attribute with Enum

My enums are None, which means no value has been selected yet. Nothing should be stored in the database.

enum MyEnum
{
    None = 0,
    SomeValue = 1,
...
}

      

Is there a way that I can use the standard Data annotation expression such that validation fails if None is selected?

+3


source to share


2 answers


You will have a better chance of being restricted [Range(SomeValue, LastValue)]

.



+2


source


I would be tempted to do the following if all of your enums define None to be 0:



 [RegularExpression("[1-9][0-9]*", ErrorMessage = "None should never be saved to database.")]
 public MyEnum val { get; set; }

      

0


source







All Articles