예제 #1
0
        private void ValidateProperty(IPropertyMetadata property)
        {
            var context = new ValidationContext(Entity, _serviceProvider, null)
            {
                MemberName = property.PropertyName
            };
            var results = new List <ValidationResult>();

            try
            {
                // even though this says Try, and therefore it should not throw an error, IT DOES when a given property is not part of Entity
                Validator.TryValidateProperty(property.Getter(Entity), context, results);
            }
            catch
            {
            }

            foreach (var result in DataProviders.Where(p => p.Property == property).SelectMany(p => p.Validate(Entity, _serviceProvider)))
            {
                results.Add(result);
            }

            var state = GetPropertyState(property);

            state.ClearMessages();
            state.WasValidated = true;

            foreach (var result in results)
            {
                state.AddMessage(result.ErrorMessage);
            }

            OnValidationStateChanged?.Invoke(this, new ValidationStateChangedEventArgs(isValid: !HasValidationMessages()));
        }
예제 #2
0
        private void ValidateModel()
        {
            var context = new ValidationContext(Entity, _serviceProvider, null);
            var results = new List <ValidationResult>();

            try
            {
                // even though this says Try, and therefore it should not throw an error, IT DOES when a given property is not part of Entity
                Validator.TryValidateObject(Entity, context, results, true);
            }
            catch
            {
            }

            ClearAllFieldStates();

            _fieldStates
            .Where(kv => kv.Value.IsBusy)
            .ForEach(kv => results.Add(new ValidationResult(
                                           $"The {kv.Key.PropertyName} field indicates it is performing an asynchronous task which must be awaited.",
                                           new[] { kv.Key.PropertyName })));

            foreach (var result in DataProviders.SelectMany(p => p.Validate(Entity, _serviceProvider)))
            {
                results.Add(result);
            }

            foreach (var result in results)
            {
                if (!result.MemberNames.Any())
                {
                    throw new InvalidOperationException("Only validators which explicitly specify which member is invalid should be used.");
                }

                result.MemberNames.ForEach(name => GetFieldState(name)?.AddMessage(result.ErrorMessage));
            }

            _fieldStates.ForEach(kv => kv.Value.WasValidated = true);

            OnValidationStateChanged?.Invoke(this, new ValidationStateChangedEventArgs(isValid: !HasValidationMessages()));
        }
예제 #3
0
 public void NotifyPropertyFinished(IPropertyMetadata property)
 {
     GetPropertyState(property) !.IsBusy = false;
     OnValidationStateChanged?.Invoke(this, new ValidationStateChangedEventArgs());
 }
예제 #4
0
 public void NotifyPropertyBusy(IPropertyMetadata property)
 {
     GetPropertyState(property).IsBusy = true;
     OnValidationStateChanged?.Invoke(this, new ValidationStateChangedEventArgs(false));
 }
예제 #5
0
 private void OnValidationStateChangedHandler(object sender, ValidationStateChangedEventArgs e) => InvokeAsync(() => OnValidationStateChanged.InvokeAsync(e));