Reputation: 132558
I have a custom ValidationAttribute
, however I only want to validate this property if a CheckBox is checked.
I've made my class inherit from IValidationObject
and am using the Validate
method to perform any custom validation, however can I use a custom ValidationAttribute
here instead of duplicating the code? And if so, how?
public class MyClass : IValidatableObject
{
public bool IsReminderChecked { get; set; }
public bool EmailAddress { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (IsReminderChecked)
{
// How can I validate the EmailAddress field using
// the Custom Validation Attribute found below?
}
}
}
// Custom Validation Attribute - used in more than one place
public class EmailValidationAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
var email = value as string;
if (string.IsNullOrEmpty(email))
return false;
try
{
var testEmail = new MailAddress(email).Address;
}
catch (FormatException)
{
return false;
}
return true;
}
}
Upvotes: 2
Views: 6322
Reputation: 3289
It's possible to validate a property based on the value of another property, but there are a few hoops to jump through to make sure the validation engine works the way you expect. Simon Ince's RequiredIfAttribute has a good approach and it should be easy to modify it into a ValidateEmailIfAttribute
just by adding your e-mail validation logic to the IsValid
method.
For example, you could have your base validation attribute, just like you do now:
public class ValidateEmailAttribute : ValidationAttribute
{
...
}
and then define the conditional version, using Ince's approach:
public class ValidateEmailIfAttribute : ValidationAttribute, IClientValidatable
{
private ValidateEmailAttribute _innerAttribute = new ValidateEmailAttribute();
public string DependentProperty { get; set; }
public object TargetValue { get; set; }
public ValidateEmailIfAttribute(string dependentProperty, object targetValue)
{
this.DependentProperty = dependentProperty;
this.TargetValue = targetValue;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// get a reference to the property this validation depends upon
var containerType = validationContext.ObjectInstance.GetType();
var field = containerType.GetProperty(this.DependentProperty);
if (field != null)
{
// get the value of the dependent property
var dependentvalue = field.GetValue(validationContext.ObjectInstance, null);
// compare the value against the target value
if ((dependentvalue == null && this.TargetValue == null) ||
(dependentvalue != null && dependentvalue.Equals(this.TargetValue)))
{
// match => means we should try validating this field
if (!_innerAttribute.IsValid(value))
// validation failed - return an error
return new ValidationResult(this.ErrorMessage, new[] { validationContext.MemberName });
}
}
return ValidationResult.Success;
}
// Client-side validation code omitted for brevity
}
Then you could just have something like:
[ValidateEmailIf("IsReminderChecked", true)]
public bool EmailAddress { get; set; }
Upvotes: 6