| | | 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.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Linq.Expressions; |
| | | 9 | | using ReactiveUI.Validation.Collections; |
| | | 10 | | using ReactiveUI.Validation.Components.Abstractions; |
| | | 11 | | |
| | | 12 | | namespace ReactiveUI.Validation.Components |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc cref="ReactiveObject" /> |
| | | 15 | | /// <inheritdoc cref="IValidationComponent" /> |
| | | 16 | | /// <inheritdoc cref="IDisposable" /> |
| | | 17 | | /// <summary> |
| | | 18 | | /// More generic observable for determination of validity. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// Validates a single property. Though in the passed validityObservable more properties can be referenced. |
| | | 22 | | /// We probably need a more 'complex' one, where the params of the validation block are |
| | | 23 | | /// passed through? |
| | | 24 | | /// Also, what about access to the view model to output the error message?. |
| | | 25 | | /// </remarks> |
| | | 26 | | public class ModelObservableValidation<TViewModel, TViewModelProp> : ModelObservableValidationBase<TViewModel> |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel, TProperty1}"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 32 | | /// <param name="viewModelProperty">ViewModel property referenced in validityObservable.</param> |
| | | 33 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 34 | | /// <param name="message">Func to define the validation error message based on the viewModelProperty value.</par |
| | | 35 | | public ModelObservableValidation( |
| | | 36 | | TViewModel viewModel, |
| | | 37 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 38 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 39 | | string message) |
| | | 40 | | : this(viewModel, viewModelProperty, validityObservable, (p, isValid) => |
| | | 41 | | new ValidationText(isValid ? string.Empty : message)) |
| | | 42 | | { |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel, TProperty1}"/> class. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 49 | | /// <param name="viewModelProperty">ViewModel property referenced in validityObservable.</param> |
| | | 50 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 51 | | /// <param name="message">Func to define the validation error message based on the viewModelProperty value.</par |
| | | 52 | | public ModelObservableValidation( |
| | | 53 | | TViewModel viewModel, |
| | | 54 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 55 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 56 | | Func<TViewModel, string> message) |
| | | 57 | | : this(viewModel, viewModelProperty, validityObservable, (p, isValid) => |
| | | 58 | | new ValidationText(isValid ? string.Empty : message(p))) |
| | | 59 | | { |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel, TProperty1}"/> class. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 66 | | /// <param name="viewModelProperty">ViewModel property referenced in validityObservable.</param> |
| | | 67 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 68 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and validityObs |
| | | 69 | | public ModelObservableValidation( |
| | | 70 | | TViewModel viewModel, |
| | | 71 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 72 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 73 | | Func<TViewModel, bool, string> messageFunc) |
| | | 74 | | : this(viewModel, viewModelProperty, validityObservable, (vm, state) => |
| | | 75 | | new ValidationText(messageFunc(vm, state))) |
| | | 76 | | { |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel, TProperty1}"/> class. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 83 | | /// <param name="viewModelProperty">ViewModel property referenced in validityObservable.</param> |
| | | 84 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 85 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and validityObs |
| | | 86 | | private ModelObservableValidation( |
| | | 87 | | TViewModel viewModel, |
| | | 88 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 89 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 90 | | Func<TViewModel, bool, ValidationText> messageFunc) |
| | | 91 | | : base(viewModel, validityObservable, messageFunc) |
| | | 92 | | { |
| | | 93 | | // record this property name |
| | | 94 | | AddProperty(viewModelProperty); |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc cref="ReactiveObject" /> |
| | | 99 | | /// <inheritdoc cref="IValidationComponent" /> |
| | | 100 | | /// <inheritdoc cref="IDisposable" /> |
| | | 101 | | /// <summary> |
| | | 102 | | /// More generic observable for determination of validity. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <remarks> |
| | | 105 | | /// for backwards compatibility, validated properties are not explicitly defined, so we don't really know what's ins |
| | | 106 | | /// </remarks> |
| | | 107 | | [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification = "Sa |
| | | 108 | | public class ModelObservableValidation<TViewModel> : ModelObservableValidationBase<TViewModel> |
| | | 109 | | { |
| | | 110 | | /// <summary> |
| | | 111 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel}"/> class. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 114 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 115 | | /// <param name="message">Func to define the validation error message based on the viewModelProperty value.</par |
| | | 116 | | public ModelObservableValidation( |
| | | 117 | | TViewModel viewModel, |
| | | 118 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 119 | | string message) |
| | 0 | 120 | | : this(viewModel, validityObservable, (p, isValid) => |
| | 0 | 121 | | new ValidationText(isValid ? string.Empty : message)) |
| | | 122 | | { |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel}"/> class. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 129 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 130 | | /// <param name="message">Func to define the validation error message based on the viewModelProperty value.</par |
| | | 131 | | public ModelObservableValidation( |
| | | 132 | | TViewModel viewModel, |
| | | 133 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 134 | | Func<TViewModel, string> message) |
| | 0 | 135 | | : this(viewModel, validityObservable, (p, isValid) => |
| | 0 | 136 | | new ValidationText(isValid ? string.Empty : message(p))) |
| | | 137 | | { |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel}"/> class. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 144 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 145 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and validityObs |
| | | 146 | | public ModelObservableValidation( |
| | | 147 | | TViewModel viewModel, |
| | | 148 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 149 | | Func<TViewModel, bool, string> messageFunc) |
| | 8 | 150 | | : this(viewModel, validityObservable, (vm, state) => new ValidationText(messageFunc(vm, state))) |
| | | 151 | | { |
| | 4 | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Initializes a new instance of the <see cref="ModelObservableValidation{TViewModel}"/> class. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 158 | | /// <param name="validityObservable">Observable to define if the viewModel is valid or not.</param> |
| | | 159 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and validityObs |
| | | 160 | | private ModelObservableValidation( |
| | | 161 | | TViewModel viewModel, |
| | | 162 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 163 | | Func<TViewModel, bool, ValidationText> messageFunc) |
| | 4 | 164 | | : base(viewModel, validityObservable, messageFunc) |
| | | 165 | | { |
| | 4 | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |