0

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?

KentZhou
  • 24,805
  • 41
  • 134
  • 200

1 Answers1

0

I believe you need Class-level validation. Have a look at this question Of course, your code need to be compiled client-side. (If using WCF ria services there are a couple of ways to reach this)

HTH

Community
  • 1
  • 1
mCasamento
  • 1,413
  • 1
  • 11
  • 21