Custom DataAnnotation?

I would like to set the DataAnnotation for the view model to a dynamic value that is configured through web.config. In the following example, I get this error: "The attribute argument must be a constant expression, an expression of typeof, or an expression to create an array of attribute parameter type." Does anyone know if this is possible? Thanks

[DataType(DataType.Password)] [RegularExpression(Properties.Settings.Default.PasswordExpression)] public string Password { get; set; } 
+6
source share
1 answer

Attribute parameters must be constants, that is, those whose value can be resolved at compile time. But you can write your own simple attribute class that takes the element name in appSettings, gets the base value and passes it to the regular regexp processing. Then your attribute will look like this:

 [ConfigedRegularExpression("PasswordExpression")] 

where PasswordExpression is the name of the application parameter that contains the actual regular expression string.

and by writing this and doing a search (I should have done this first), I see that someone worked for you here:

How to write your own RegularExpressionValidator that takes values ​​from a configuration file?

+6
source

Source: https://habr.com/ru/post/897432/


All Articles