/// <summary> /// Invokes the validation on the specified context with the specified parameters /// </summary> /// <param name="sender">The property that invoked the validation</param> /// <param name="context">The Viewmodel context that has to be validated</param> /// <param name="propertyInfo">The <see cref="PropertyInfo"/> of the validated property</param> /// <param name="value">The value of the property</param> /// <returns>Has to return true on validation error otherwise false</returns> protected override Task <bool> OnValidateAsync(PropertyInfo sender, IValidatableViewModel context, PropertyInfo propertyInfo, object value) { // if the value is null, then we should return a validation successfull, so that it is // possible to have non mandatory inputs if (value == null) { return(Task.FromResult(false)); } if (this.value != null) { return(Task.FromResult(!Comparer.GreaterThanOrEqual(value, this.value))); } var otherProperty = context.GetType().GetPropertyEx(this.propertyName); if (otherProperty == null) { throw new ArgumentException(string.Format("The property '{0}' was not found on '{1}'.", this.propertyName, context.GetType().FullName)); } // Also same for this value var comparisonValue = otherProperty.GetValue(context); if (comparisonValue == null) { return(Task.FromResult(false)); } return(Task.FromResult(!Comparer.GreaterThanOrEqual(value, comparisonValue))); }
/// <summary> /// Occures on validation /// <para/> /// Can be used to modify the validation error message. /// </summary> /// <param name="errorMessage">The validation error message</param> /// <param name="context">The Viewmodel context that has to be validated</param> /// <returns>A modified validation error message</returns> protected override string ValidationMessage(string errorMessage, IValidatableViewModel context) { var otherProperty = context.GetType().GetPropertyEx(this.otherProperty); if (otherProperty == null) { return(errorMessage); } return(errorMessage.ToString(otherProperty.GetValue(context))); }
/// <summary> /// Invokes the validation on the specified context with the specified parameters /// </summary> /// <param name="sender">The property that invoked the validation</param> /// <param name="context">The viewmodel context that has to be validated</param> /// <param name="propertyInfo">The <see cref="PropertyInfo"/> of the validated property</param> /// <param name="value">The value of the property</param> /// <returns>Has to return true on validation error otherwise false</returns> protected override async Task <bool> OnValidateAsync(PropertyInfo sender, IValidatableViewModel context, PropertyInfo propertyInfo, object value) { // if the value is null, then we should return a validation successfull, so that it is // possible to have non mandatory inputs if (value == null) { return(false); } var secondProperty = context.GetType().GetPropertyEx(this.otherProperty); if (secondProperty == null) { return(true); } var secondValue = secondProperty.GetValue(context); if (sender == null) { await context.ValidateAsync(propertyInfo, secondProperty.Name); } if (value is SecureString && (secondValue is SecureString || secondValue == null)) { var ssa = value as SecureString; var ssb = secondValue as SecureString; if (ssa.Length == 0 && ssb == null) { return(true); } if (ssa.Length != 0 && ssb == null) { return(false); } return(ssa.IsEqualTo(ssb)); } if (secondValue == null) { return(false); } return(!Comparer.Equals(value, secondValue)); }