I have a complete validation on an obeject and am trying to figure out the best way to handle it.
Given the following class:
public class LetterResponse {
public Guid Id {get;set;}
public bool SendBlankCart {get;set;}
public string ToName {get;set;}
public string ToAddress {get;set;}
}
I want to use a dataannotation and xval in order to validate the class before I persist it, but I have complex validation that requires more than one property.
Pseudo:
if SendBlankCart {
- no validation on ToName, ToAddress
} else {
ToName - required.
ToAddress - required.
}
I would like to annotate like this:
[LetterResponseValidator]
public class LetterResponse {
public Guid Id {get;set;}
public bool SendBlankCart {get;set;}
public string ToName {get;set;}
public string ToAddress {get;set;}
}
and have a validation rule like this:
public class LetterResponseValidator : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value.GetType() == typeof(LetterResponse))
{
var letterResponse = (letterResponse) value;
if (letterResponse.SendBlankCard)
{
return true;
} else
{
if (string.IsNullOrEmpty(letterResponse.FromDisplayName) || string.IsNullOrEmpty(letterResponse.ToAddress1))
{
return false;
}
return true;
}
}
return false;
}
}
I am expecting the parameter to be my instance of the LetterResponse class, but it never gets called on my validation runner?
Does anyone know a way to handle this?
Thanks,
Hal