コード例 #1
0
ファイル: CachedValidation.cs プロジェクト: the-nerbs/MvvmLib
        /// <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);
        }
コード例 #2
0
        /// <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));
            }
        }
コード例 #3
0
ファイル: BrewgrController.cs プロジェクト: ric3ca/brewgr.com
        /// <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);
        }
コード例 #4
0
ファイル: BrewgrController.cs プロジェクト: ric3ca/brewgr.com
 /// <summary>
 /// Validates and forwards any error messages
 /// </summary>
 protected bool ValidateAndForwardMessages(IValidatingViewModel validatingViewModel)
 {
     return(ValidateAndMessage(validatingViewModel, this.ForwardMessage));
 }
コード例 #5
0
ファイル: BrewgrController.cs プロジェクト: ric3ca/brewgr.com
 /// <summary>
 /// Validates and appends any error messages
 /// </summary>
 protected bool ValidateAndAppendMessages(IValidatingViewModel validatingViewModel)
 {
     return(this.ValidateAndMessage(validatingViewModel, this.AppendMessage));
 }
コード例 #6
0
ファイル: BrewgrController.cs プロジェクト: ric3ca/brewgr.com
 /// <summary>
 /// Validates a model without messaging
 /// </summary>
 protected bool ValidateWithoutMessaging(IValidatingViewModel validatingViewModel)
 {
     return(this.ValidateAndMessage(validatingViewModel));
 }
コード例 #7
0
 public IEnumerable <ValidationRuleResult> Validate(IValidatingViewModel viewModel, string propertyName)
 {
     return(Array.Empty <ValidationRuleResult>());
 }
コード例 #8
0
 /// <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));
 }