Configurable ASP.NET MVC Validations

I would like to use asp.net mvc client / server verification coming from a custom source.

Some people like the .config file, where I can post information: Type, Member, ValidationType

<validations> <add type="Customer" member="Name" validator="Required" /> <add type="Customer" member="Age" validator="Range" mimimum="18" maximum="100" /> </validations> 

With this plan, you could enable / disable checks.

Any idea?

+4
source share
1 answer

If you need this, consider another advanced validation structure, such as the Enterprise Library Health Validator.

If you want to do it yourself, I would suggest creating your own attribute that would be inherited from ValidationAttribute, like this (partially pseudo-code, I'm sure you get the idea)

 public class ConfigurableValidationAttribute: ValidationAttribute { public override bool IsValid(object value) { string objectType = value.GetType().FullName; string objectName = GetMyObjectName(value); // interface? reflection? var validationRules = GetValidationRulesFor(objectType, name); // from your configuration foreach (var rule in validationRules) { ValidationAttribute attr = null; switch (rule.ValidatorName) { case "Required": attr = new RequiredAttribute(); case "StringLength": attr = // you get the idea } if (!attr.IsValid(value)) return false; } return true; } } 
+6
source

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


All Articles