Ну, на мой взгляд, вам понадобится CompositionType.Or и пользовательский валидатор, который отменяет значение поля bool:
[ValidatorComposition(CompositionType.Or)]
[FalseFieldValidator("EnabledField")]
[RegexValidator("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", MessageTemplate = "Invalid Email Address")]
public String Email_NotificationUser { get; set; }
Тогда упрощенный атрибут валидации и логический код будут иметь вид:
[AttributeUsage(AttributeTargets.Property)]
public class FalseFieldValidatorAttribute: ValidatorAttribute {
protected override Validator DoCreateValidator(Type targetType) {
return new FalseFieldValidator(FieldName);
}
protected string FieldName { get; set; }
public FalseFieldValidatorAttribute(string fieldName) {
this.FieldName = fieldName;
}
}
public class FalseFieldValidator: Microsoft.Practices.EnterpriseLibrary.Validation.Validator {
protected override string DefaultMessageTemplate {
get { return ""; }
}
protected string FieldName { get; set; }
public FalseFieldValidator(string fieldName) : base(null, null) {
FieldName = fieldName;
}
public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults) {
System.Reflection.PropertyInfo propertyInfo = currentTarget.GetType().GetProperty(FieldName);
if(propertyInfo != null) {
if((bool)propertyInfo.GetValue(currentTarget, null))
validationResults.AddResult(new Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult(String.Format("{0} value is True", FieldName), currentTarget, key, null, this));
}
}
}
В этом сценарии FalseFieldValidator
завершится ошибкой всякий раз, когда EnabledField имеет значение true, а условие «или» даст RegexValidator
шанс на выстрел.