/// <summary> /// Validates the given property. /// </summary> /// <param name="viewModel">The view model being validated.</param> /// <param name="propertyName">The name of the property being validated.</param> /// <returns> /// A collection of <see cref="ValidationRuleResult"/> objects describing validation /// failures. This does not include successful results. /// </returns> public IEnumerable <ValidationRuleResult> Validate(IValidatingViewModel viewModel, string propertyName) { IEnumerable <ValidationRuleResult> results; if (!_results.TryGetValue(propertyName, out results)) { results = ValidationUtilities.Validate(viewModel, propertyName).ToArray(); _results.TryAdd(propertyName, results); } return(results); }
/// <summary> /// Validates a property of a view model. /// </summary> /// <param name="viewModel">The view model.</param> /// <param name="propertyName">The name of the property to validate.</param> /// <returns> /// A collection of <see cref="ValidationRuleResult"/> objects describing validation /// failures. This does not include successful results. /// </returns> public static IEnumerable <ValidationRuleResult> Validate( IValidatingViewModel viewModel, string propertyName) { IValidationRule[] rules = viewModel.Rules[propertyName].ToArray(); object value = default; Exception getValueError = null; if (string.IsNullOrEmpty(propertyName)) { value = viewModel; } else { Func <object, object> getter = viewModel.GetterCache[propertyName]; try { value = getter(viewModel); } catch (Exception ex) { getValueError = ex; } } if (getValueError is null) { foreach (IValidationRule rule in rules.ToArray()) { ValidationRuleResult result; try { result = rule.Run(value); } catch (Exception ex) { result = new ValidationRuleResult(true, ex.Message); } if (!(result is null) && result.IsError) { yield return(result); } } } else { yield return(new ValidationRuleResult(true, getValueError.Message)); } }
/// <summary> /// Validates and Messages /// </summary> protected bool ValidateAndMessage(IValidatingViewModel validatingViewModel, Action <IMessage> messagingAction = null) { var result = validatingViewModel.Validate(); if (!result.IsValid) { if (messagingAction != null) { result.Errors.ForEach(x => messagingAction(new ErrorMessage { Text = x.ErrorMessage })); } return(false); } return(true); }
/// <summary> /// Validates and forwards any error messages /// </summary> protected bool ValidateAndForwardMessages(IValidatingViewModel validatingViewModel) { return(ValidateAndMessage(validatingViewModel, this.ForwardMessage)); }
/// <summary> /// Validates and appends any error messages /// </summary> protected bool ValidateAndAppendMessages(IValidatingViewModel validatingViewModel) { return(this.ValidateAndMessage(validatingViewModel, this.AppendMessage)); }
/// <summary> /// Validates a model without messaging /// </summary> protected bool ValidateWithoutMessaging(IValidatingViewModel validatingViewModel) { return(this.ValidateAndMessage(validatingViewModel)); }
public IEnumerable <ValidationRuleResult> Validate(IValidatingViewModel viewModel, string propertyName) { return(Array.Empty <ValidationRuleResult>()); }
/// <summary> /// Gets a collection of validation results /// </summary> /// <param name="viewModel">The view model whose property to validate.</param> /// <param name="propertyName">The name of the property, or the empty string for entity rules.</param> /// <returns> /// A collection of <see cref="ValidationRuleResult"/> objects describing validation /// failures. This does not include successful results. /// </returns> public IEnumerable <ValidationRuleResult> Validate( IValidatingViewModel viewModel, string propertyName) { return(ValidationUtilities.Validate(viewModel, propertyName)); }