/// <summary> /// Determines if the value specified in the ValidateIfMemberValueIsValue is a valid value. /// </summary> /// <param name="sender">The sender.</param> /// <returns></returns> /// <exception cref="System.ArgumentException">Can not base validation off of a non-boolean property.</exception> public bool CanValidate(IValidatable sender) { if (string.IsNullOrEmpty(this.ValidateIfMemberValueIsValid)) { return(true); } string valueToParse = string.Empty; bool evaluateInverseValue = false; if (this.ValidateIfMemberValueIsValid.StartsWith("!")) { evaluateInverseValue = true; valueToParse = this.ValidateIfMemberValueIsValid.Substring(1); } else { valueToParse = this.ValidateIfMemberValueIsValid; } bool result = false; object valueToCompare = this.GetComparisonValue(sender, valueToParse); if (valueToCompare is bool) { bool.TryParse(valueToCompare.ToString(), out result); } else if (valueToCompare is string) { // We can not validate if the string is empty. result = !string.IsNullOrWhiteSpace(valueToCompare.ToString()); } else if (valueToCompare is int || valueToCompare is short || valueToCompare is long || valueToCompare is double || valueToCompare is float || valueToCompare is decimal) { var numberGreaterThanRule = new ValidateNumberIsGreaterThanAttribute(); numberGreaterThanRule.GreaterThanValue = "0"; numberGreaterThanRule.ValidationMessageType = this.ValidationMessageType; numberGreaterThanRule.FailureMessage = this.FailureMessage; PropertyInfo property = this.GetAlternatePropertyInfo(sender, ValidateIfMemberValueIsValid); IValidationMessage validationMessage = numberGreaterThanRule.Validate(property, sender); // if we are greater than 0, then we hav a valid value and can validate. result = validationMessage == null; } else if (valueToCompare == null) { // We can not validate if the object is null. result = false; } else { result = true; } return(evaluateInverseValue ? !result : result); }
/// <summary> /// Determines if the value specified in the ValidateIfMemberValueIsValue is a valid value. /// </summary> /// <param name="sender">The sender.</param> /// <returns></returns> /// <exception cref="System.ArgumentException">Can not base validation off of a non-boolean property.</exception> public bool CanValidate(IValidatable sender) { if (string.IsNullOrEmpty(this.ValidateIfMemberValueIsValid)) { return true; } string valueToParse = string.Empty; bool evaluateInverseValue = false; if (this.ValidateIfMemberValueIsValid.StartsWith("!")) { evaluateInverseValue = true; valueToParse = this.ValidateIfMemberValueIsValid.Substring(1); } else { valueToParse = this.ValidateIfMemberValueIsValid; } bool result = false; object valueToCompare = this.GetComparisonValue(sender, valueToParse); if (valueToCompare is bool) { bool.TryParse(valueToCompare.ToString(), out result); } else if (valueToCompare is string) { // We can not validate if the string is empty. result = !string.IsNullOrWhiteSpace(valueToCompare.ToString()); } else if (valueToCompare is int || valueToCompare is short || valueToCompare is long || valueToCompare is double || valueToCompare is float || valueToCompare is decimal) { var numberGreaterThanRule = new ValidateNumberIsGreaterThanAttribute(); numberGreaterThanRule.GreaterThanValue = "0"; numberGreaterThanRule.ValidationMessageType = this.ValidationMessageType; numberGreaterThanRule.FailureMessage = this.FailureMessage; PropertyInfo property = this.GetAlternatePropertyInfo(sender, ValidateIfMemberValueIsValid); IValidationMessage validationMessage = numberGreaterThanRule.Validate(property, sender); // if we are greater than 0, then we hav a valid value and can validate. result = validationMessage == null; } else if (valueToCompare == null) { // We can not validate if the object is null. result = false; } else { result = true; } return evaluateInverseValue ? !result : result; }