protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var memberNames = new[] { validationContext.MemberName };
PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty);
if (otherPropertyInfo == null)
{
return new ValidationResult(CultureInfo.CurrentCulture.Format("Could not find a property named {0}.", OtherProperty), memberNames);
}
var displayAttribute =
otherPropertyInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;
if (displayAttribute != null && !string.IsNullOrWhiteSpace(displayAttribute.Name))
{
OtherPropertyDisplayName = displayAttribute.Name;
}
object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
if (!Equals(value, otherPropertyValue))
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), memberNames);
}
return null;
}