Suppose I have an entity Person(id, dept, EmailAddress,DOB, ...), when model created with EF, then create a metadata class for this class to put validation rule on server side like:
[CustomValidation(typeof(MyValidator), "DOBValidator")]
public Nullable<DateTime> DOB { get; set; }
[RegularExpression("^([\\w-\\.]+)@((\\[[0–9]{1,3}\\.[0–9]{1,3}\\.[0–9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4" + "}|[0–9]{1,3})(\\]?)$", ErrorMessage = "Invalid email address")]
[StringLength(128)]
public string EmailAddress { get; set; }
when the validation rule is in place, for any data sent from client side will go through the validation with no exception when submit any data for saving.
but now I want exception for the rule: from UI, when get data from UI by binding for entity Person, based on the data, I want to ignore validation. for example, when Dept=A, do not check EmailAddress validation, for dept=B, do not check DOB validation.
How to resolve this issue?