How to validate one field related to another’s value in ASP .NET MVC 3
One possibility is to write a custom validation attribute: public class RequiredIfOtherFieldIsNullAttribute : ValidationAttribute, IClientValidatable { private readonly string _otherProperty; public RequiredIfOtherFieldIsNullAttribute(string otherProperty) { _otherProperty = otherProperty; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var property = validationContext.ObjectType.GetProperty(_otherProperty); if (property == null) { return new ValidationResult(string.Format( CultureInfo.CurrentCulture, “Unknown property {0}”, new[] { _otherProperty … Read more