Making exceptions for ValidationAttribute

itnifl used Ask the Experts™
So I have made a custom validation attribute for a property in my C# MVC view model, and it works as expected on the server side. But now I want the user to be able to cancel the validation by selecting a checkbox that says; skip verification, and then let the whole system store the value in the property anyway. But only for that specific selected property, all other properties should be validated as normal.

Any good suggestions and examples on how to do this?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixed it myself, did it like this:
public class VerifyADConnectionStringAttribute : ValidationAttribute
      protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
         string errorMessage;
         bool skipValidation = false;
         var WebPageConfigurationModel = validationContext.ObjectInstance as Models.View.WebPageConfiguration;
         if (WebPageConfigurationModel != null) {
            skipValidation = WebPageConfigurationModel.ADConnectionStringExemption;
         if (!skipValidation) {
//Validate here, return whatever result.
//return success here if we get this far.

Open in new window




Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial