| | | 1 | | // Copyright (c) 2020 .NET Foundation and Contributors. All rights reserved. |
| | | 2 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 3 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 4 | | // See the LICENSE file in the project root for full license information. |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Collections; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.ComponentModel; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Reactive.Concurrency; |
| | | 12 | | using System.Reactive.Linq; |
| | | 13 | | using DynamicData; |
| | | 14 | | using DynamicData.Binding; |
| | | 15 | | using ReactiveUI.Validation.Abstractions; |
| | | 16 | | using ReactiveUI.Validation.Components.Abstractions; |
| | | 17 | | using ReactiveUI.Validation.Contexts; |
| | | 18 | | using ReactiveUI.Validation.States; |
| | | 19 | | |
| | | 20 | | namespace ReactiveUI.Validation.Helpers |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Base class for ReactiveObjects that support INotifyDataErrorInfo validation. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <typeparam name="TViewModel">The parent view model.</typeparam> |
| | | 26 | | public abstract class ReactiveValidationObject<TViewModel> : ReactiveObject, IValidatableViewModel, INotifyDataError |
| | | 27 | | { |
| | | 28 | | private bool _hasErrors; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="ReactiveValidationObject{TViewModel}"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="scheduler">Scheduler for OAPHs and for the the ValidationContext.</param> |
| | 12 | 34 | | protected ReactiveValidationObject(IScheduler? scheduler = null) |
| | | 35 | | { |
| | 12 | 36 | | ValidationContext = new ValidationContext(scheduler); |
| | 12 | 37 | | ValidationContext.Validations |
| | 12 | 38 | | .ToObservableChangeSet() |
| | 12 | 39 | | .ToCollection() |
| | 28 | 40 | | .Select(components => components |
| | 48 | 41 | | .Select(component => component.ValidationStatusChange) |
| | 28 | 42 | | .Merge()) |
| | 12 | 43 | | .Switch() |
| | 12 | 44 | | .Subscribe(OnValidationStatusChange); |
| | 12 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged; |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public bool HasErrors |
| | | 52 | | { |
| | 20 | 53 | | get => _hasErrors; |
| | 38 | 54 | | private set => this.RaiseAndSetIfChanged(ref _hasErrors, value); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 120 | 58 | | public ValidationContext ValidationContext { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Returns a collection of error messages, required by the INotifyDataErrorInfo interface. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="propertyName">Property to search error notifications for.</param> |
| | | 64 | | /// <returns>A list of error messages, usually strings.</returns> |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public virtual IEnumerable GetErrors(string propertyName) => |
| | 24 | 67 | | string.IsNullOrEmpty(propertyName) ? |
| | 24 | 68 | | SelectInvalidPropertyValidations() |
| | 0 | 69 | | .SelectMany(validation => validation.Text) |
| | 24 | 70 | | .ToArray() : |
| | 24 | 71 | | SelectInvalidPropertyValidations() |
| | 44 | 72 | | .Where(validation => validation.ContainsPropertyName(propertyName)) |
| | 38 | 73 | | .SelectMany(validation => validation.Text) |
| | 24 | 74 | | .ToArray(); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Raises the <see cref="ErrorsChanged"/> event. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="propertyName">The name of the validated property.</param> |
| | | 80 | | protected void RaiseErrorsChanged(string propertyName = "") => |
| | 38 | 81 | | ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Selects validation components that are invalid. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <returns>Returns the invalid property validations.</returns> |
| | | 87 | | private IEnumerable<IPropertyValidationComponent<TViewModel>> SelectInvalidPropertyValidations() => |
| | 24 | 88 | | ValidationContext.Validations |
| | 24 | 89 | | .OfType<IPropertyValidationComponent<TViewModel>>() |
| | 52 | 90 | | .Where(validation => !validation.IsValid); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Updates the <see cref="HasErrors" /> property before raising the <see cref="ErrorsChanged" /> |
| | | 94 | | /// event, and then raises the <see cref="ErrorsChanged" /> event. This behaviour is required by WPF, see: |
| | | 95 | | /// https://stackoverflow.com/questions/24518520/ui-not-calling-inotifydataerrorinfo-geterrors/24837028. |
| | | 96 | | /// </summary> |
| | | 97 | | private void OnValidationStatusChange(ValidationState state) |
| | | 98 | | { |
| | 38 | 99 | | HasErrors = !ValidationContext.GetIsValid(); |
| | 38 | 100 | | if (state.Component is IPropertyValidationComponent<TViewModel> propertyValidationComponent && |
| | 38 | 101 | | propertyValidationComponent.PropertyCount == 1) |
| | | 102 | | { |
| | 38 | 103 | | var propertyName = propertyValidationComponent.Properties.First(); |
| | 38 | 104 | | RaiseErrorsChanged(propertyName); |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | RaiseErrorsChanged(); |
| | | 109 | | } |
| | 0 | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |