I have implemented IValidatableObject
several times and have never found out what the purpose of parsing ValidationContext
to the Validate method is - my typical IValidatableObject
implementation looks something like this:
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Prop1 == Prop2)
{
yield return new ValidationResult(
"Prop1 and Prop2 must be different.",
new[] {"Prop1", "Prop2"});
}
}
Is there anything that I have missed that I could have used validationContext
for?
EDIT: I'm using ASP.NET MVC and this is implemented in the class - not in the controller.
ValidationContext
contains IServiceProvider
property. It is extension point to pass DI container to your validation attributes and Validate methods.
You can use it, as example, to validate against database without setting dependency on dbcontext in your model.