How do I use IValidatableObject?

前端 未结 7 1765
我在风中等你
我在风中等你 2020-11-22 02:59

I understand that IValidatableObject is used to validate an object in a way that lets one compare properties against each other.

I\'d still like to have

7条回答
  •  执笔经年
    2020-11-22 03:38

    I liked cocogza's answer except that calling base.IsValid resulted in a stack overflow exception as it would re-enter the IsValid method again and again. So I modified it to be for a specific type of validation, in my case it was for an e-mail address.

    [AttributeUsage(AttributeTargets.Property)]
    class ValidEmailAddressIfTrueAttribute : ValidationAttribute
    {
        private readonly string _nameOfBoolProp;
    
        public ValidEmailAddressIfTrueAttribute(string nameOfBoolProp)
        {
            _nameOfBoolProp = nameOfBoolProp;
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (validationContext == null)
            {
                return null;
            }
    
            var property = validationContext.ObjectType.GetProperty(_nameOfBoolProp);
            if (property == null)
            {
                return new ValidationResult($"{_nameOfBoolProp} not found");
            }
    
            var boolVal = property.GetValue(validationContext.ObjectInstance, null);
    
            if (boolVal == null || boolVal.GetType() != typeof(bool))
            {
                return new ValidationResult($"{_nameOfBoolProp} not boolean");
            }
    
            if ((bool)boolVal)
            {
                var attribute = new EmailAddressAttribute {ErrorMessage = $"{value} is not a valid e-mail address."};
                return attribute.GetValidationResult(value, validationContext);
            }
            return null;
        }
    }
    

    This works much better! It doesn't crash and produces a nice error message. Hope this helps someone!

提交回复
热议问题