I have a full check on obeject and I'm trying to find a better way to handle this.
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 dataannotation and xval to test a class before I save it, but I have a complex check that requires more than one property.
Pseudo:
if SendBlankCart {
- no validation on ToName, ToAddress
} else {
ToName - required.
ToAddress - required.
}
I would like to comment as follows:
[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 execute the validation rule as follows:
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 expect this parameter to be my instance of the LetterResponse class, but will it never be called in my validation runner?
Does anyone know a way to handle this?
Thanks,
Hal
source
share