| | | 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.Linq.Expressions; |
| | | 8 | | using ReactiveUI.Validation.Abstractions; |
| | | 9 | | using ReactiveUI.Validation.Components; |
| | | 10 | | using ReactiveUI.Validation.Helpers; |
| | | 11 | | |
| | | 12 | | namespace ReactiveUI.Validation.Extensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Extensions methods associated to <see cref="IValidatableViewModel"/> instances. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class ValidatableViewModelExtensions |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Setup a validation rule for a specified ViewModel property with static error message. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 23 | | /// <typeparam name="TViewModelProp">ViewModel property type.</typeparam> |
| | | 24 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 25 | | /// <param name="viewModelProperty">ViewModel property.</param> |
| | | 26 | | /// <param name="isPropertyValid">Func to define if the viewModelProperty is valid or not.</param> |
| | | 27 | | /// <param name="message">Validation error message.</param> |
| | | 28 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 29 | | public static ValidationHelper ValidationRule<TViewModel, TViewModelProp>( |
| | | 30 | | this TViewModel viewModel, |
| | | 31 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 32 | | Func<TViewModelProp, bool> isPropertyValid, |
| | | 33 | | string message) |
| | | 34 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 35 | | { |
| | 28 | 36 | | if (viewModel is null) |
| | | 37 | | { |
| | 0 | 38 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 39 | | } |
| | | 40 | | |
| | 28 | 41 | | if (viewModelProperty is null) |
| | | 42 | | { |
| | 0 | 43 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 44 | | } |
| | | 45 | | |
| | 28 | 46 | | if (isPropertyValid is null) |
| | | 47 | | { |
| | 0 | 48 | | throw new ArgumentNullException(nameof(isPropertyValid)); |
| | | 49 | | } |
| | | 50 | | |
| | 28 | 51 | | if (string.IsNullOrEmpty(message)) |
| | | 52 | | { |
| | 0 | 53 | | throw new ArgumentNullException(nameof(message)); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // We need to associate the ViewModel property |
| | | 57 | | // with something that can be easily looked up and bound to |
| | 28 | 58 | | var propValidation = new BasePropertyValidation<TViewModel, TViewModelProp>( |
| | 28 | 59 | | viewModel, |
| | 28 | 60 | | viewModelProperty, |
| | 28 | 61 | | isPropertyValid, |
| | 28 | 62 | | message); |
| | | 63 | | |
| | 28 | 64 | | viewModel.ValidationContext.Add(propValidation); |
| | 28 | 65 | | return new ValidationHelper(propValidation); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Setup a validation rule for a specified ViewModel property with dynamic error message. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 72 | | /// <typeparam name="TViewModelProp">ViewModel property type.</typeparam> |
| | | 73 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 74 | | /// <param name="viewModelProperty">ViewModel property.</param> |
| | | 75 | | /// <param name="isPropertyValid">Func to define if the viewModelProperty is valid or not.</param> |
| | | 76 | | /// <param name="message">Func to define the validation error message based on the viewModelProperty value.</par |
| | | 77 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 78 | | public static ValidationHelper ValidationRule<TViewModel, TViewModelProp>( |
| | | 79 | | this TViewModel viewModel, |
| | | 80 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 81 | | Func<TViewModelProp, bool> isPropertyValid, |
| | | 82 | | Func<TViewModelProp, string> message) |
| | | 83 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 84 | | { |
| | 4 | 85 | | if (viewModel is null) |
| | | 86 | | { |
| | 0 | 87 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 88 | | } |
| | | 89 | | |
| | 4 | 90 | | if (viewModelProperty is null) |
| | | 91 | | { |
| | 0 | 92 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 93 | | } |
| | | 94 | | |
| | 4 | 95 | | if (isPropertyValid is null) |
| | | 96 | | { |
| | 0 | 97 | | throw new ArgumentNullException(nameof(isPropertyValid)); |
| | | 98 | | } |
| | | 99 | | |
| | 4 | 100 | | if (message is null) |
| | | 101 | | { |
| | 0 | 102 | | throw new ArgumentNullException(nameof(message)); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | // We need to associate the ViewModel property |
| | | 106 | | // with something that can be easily looked up and bound to |
| | 4 | 107 | | var propValidation = new BasePropertyValidation<TViewModel, TViewModelProp>( |
| | 4 | 108 | | viewModel, |
| | 4 | 109 | | viewModelProperty, |
| | 4 | 110 | | isPropertyValid, |
| | 4 | 111 | | message); |
| | | 112 | | |
| | 4 | 113 | | viewModel.ValidationContext.Add(propValidation); |
| | 4 | 114 | | return new ValidationHelper(propValidation); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Setup a validation rule with a general observable indicating validity. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 121 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 122 | | /// <param name="viewModelObservableProperty">Func to define if the viewModel is valid or not.</param> |
| | | 123 | | /// <param name="message">Validation error message.</param> |
| | | 124 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 125 | | /// <remarks> |
| | | 126 | | /// It should be noted that the observable should provide an initial value, otherwise that can result |
| | | 127 | | /// in an inconsistent performance. |
| | | 128 | | /// </remarks> |
| | | 129 | | public static ValidationHelper ValidationRule<TViewModel>( |
| | | 130 | | this TViewModel viewModel, |
| | | 131 | | Func<TViewModel, IObservable<bool>> viewModelObservableProperty, |
| | | 132 | | string message) |
| | | 133 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 134 | | { |
| | 0 | 135 | | if (viewModel is null) |
| | | 136 | | { |
| | 0 | 137 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | if (viewModelObservableProperty is null) |
| | | 141 | | { |
| | 0 | 142 | | throw new ArgumentNullException(nameof(viewModelObservableProperty)); |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | if (message is null) |
| | | 146 | | { |
| | 0 | 147 | | throw new ArgumentNullException(nameof(message)); |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | var validation = new ModelObservableValidation<TViewModel>( |
| | 0 | 151 | | viewModel, viewModelObservableProperty, message); |
| | | 152 | | |
| | 0 | 153 | | viewModel.ValidationContext.Add(validation); |
| | 0 | 154 | | return new ValidationHelper(validation); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Setup a validation rule with a general observable indicating validity. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 161 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 162 | | /// <param name="viewModelObservableProperty">Func to define if the viewModel is valid or not.</param> |
| | | 163 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and viewModelOb |
| | | 164 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 165 | | /// <remarks> |
| | | 166 | | /// It should be noted that the observable should provide an initial value, otherwise that can result |
| | | 167 | | /// in an inconsistent performance. |
| | | 168 | | /// </remarks> |
| | | 169 | | public static ValidationHelper ValidationRule<TViewModel>( |
| | | 170 | | this TViewModel viewModel, |
| | | 171 | | Func<TViewModel, IObservable<bool>> viewModelObservableProperty, |
| | | 172 | | Func<TViewModel, string> messageFunc) |
| | | 173 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 174 | | { |
| | 0 | 175 | | if (viewModel is null) |
| | | 176 | | { |
| | 0 | 177 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (viewModelObservableProperty is null) |
| | | 181 | | { |
| | 0 | 182 | | throw new ArgumentNullException(nameof(viewModelObservableProperty)); |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | if (messageFunc is null) |
| | | 186 | | { |
| | 0 | 187 | | throw new ArgumentNullException(nameof(messageFunc)); |
| | | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | var validation = new ModelObservableValidation<TViewModel>( |
| | 0 | 191 | | viewModel, viewModelObservableProperty, messageFunc); |
| | | 192 | | |
| | 0 | 193 | | viewModel.ValidationContext.Add(validation); |
| | 0 | 194 | | return new ValidationHelper(validation); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Setup a validation rule with a general observable indicating validity. |
| | | 199 | | /// </summary> |
| | | 200 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 201 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 202 | | /// <param name="viewModelObservableProperty">Func to define if the viewModel is valid or not.</param> |
| | | 203 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and viewModelOb |
| | | 204 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 205 | | /// <remarks> |
| | | 206 | | /// It should be noted that the observable should provide an initial value, otherwise that can result |
| | | 207 | | /// in an inconsistent performance. |
| | | 208 | | /// </remarks> |
| | | 209 | | [Obsolete("This overload is planned for future removal. Consider using either the overload that accepts a " + |
| | | 210 | | "Func<TViewModel, string> as the messageFunc parameter, or the overload that accepts a string.")] |
| | | 211 | | public static ValidationHelper ValidationRule<TViewModel>( |
| | | 212 | | this TViewModel viewModel, |
| | | 213 | | Func<TViewModel, IObservable<bool>> viewModelObservableProperty, |
| | | 214 | | Func<TViewModel, bool, string> messageFunc) |
| | | 215 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 216 | | { |
| | 0 | 217 | | if (viewModel is null) |
| | | 218 | | { |
| | 0 | 219 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | if (viewModelObservableProperty is null) |
| | | 223 | | { |
| | 0 | 224 | | throw new ArgumentNullException(nameof(viewModelObservableProperty)); |
| | | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | if (messageFunc is null) |
| | | 228 | | { |
| | 0 | 229 | | throw new ArgumentNullException(nameof(messageFunc)); |
| | | 230 | | } |
| | | 231 | | |
| | 0 | 232 | | var validation = new ModelObservableValidation<TViewModel>( |
| | 0 | 233 | | viewModel, viewModelObservableProperty, messageFunc); |
| | | 234 | | |
| | 0 | 235 | | viewModel.ValidationContext.Add(validation); |
| | 0 | 236 | | return new ValidationHelper(validation); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Setup a validation rule with a general observable indicating validity. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 243 | | /// <typeparam name="TViewModelProp">ViewModel property type.</typeparam> |
| | | 244 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 245 | | /// <param name="viewModelProperty">ViewModel property referenced in viewModelObservableProperty.</param> |
| | | 246 | | /// <param name="viewModelObservableProperty">Func to define if the viewModel is valid or not.</param> |
| | | 247 | | /// <param name="message">Validation error message.</param> |
| | | 248 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 249 | | /// <remarks> |
| | | 250 | | /// It should be noted that the observable should provide an initial value, otherwise that can result |
| | | 251 | | /// in an inconsistent performance. |
| | | 252 | | /// </remarks> |
| | | 253 | | public static ValidationHelper ValidationRule<TViewModel, TViewModelProp>( |
| | | 254 | | this TViewModel viewModel, |
| | | 255 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 256 | | Func<TViewModel, IObservable<bool>> viewModelObservableProperty, |
| | | 257 | | string message) |
| | | 258 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 259 | | { |
| | 0 | 260 | | if (viewModel is null) |
| | | 261 | | { |
| | 0 | 262 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 263 | | } |
| | | 264 | | |
| | 0 | 265 | | if (viewModelProperty is null) |
| | | 266 | | { |
| | 0 | 267 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | if (viewModelObservableProperty is null) |
| | | 271 | | { |
| | 0 | 272 | | throw new ArgumentNullException(nameof(viewModelObservableProperty)); |
| | | 273 | | } |
| | | 274 | | |
| | 0 | 275 | | if (message is null) |
| | | 276 | | { |
| | 0 | 277 | | throw new ArgumentNullException(nameof(message)); |
| | | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | var validation = new ModelObservableValidation<TViewModel, TViewModelProp>( |
| | 0 | 281 | | viewModel, viewModelProperty, viewModelObservableProperty, message); |
| | | 282 | | |
| | 0 | 283 | | viewModel.ValidationContext.Add(validation); |
| | 0 | 284 | | return new ValidationHelper(validation); |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Setup a validation rule with a general observable indicating validity. |
| | | 289 | | /// </summary> |
| | | 290 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 291 | | /// <typeparam name="TViewModelProp">ViewModel property type.</typeparam> |
| | | 292 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 293 | | /// <param name="viewModelProperty">ViewModel property referenced in viewModelObservableProperty.</param> |
| | | 294 | | /// <param name="viewModelObservableProperty">Func to define if the viewModel is valid or not.</param> |
| | | 295 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and viewModelOb |
| | | 296 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 297 | | /// <remarks> |
| | | 298 | | /// It should be noted that the observable should provide an initial value, otherwise that can result |
| | | 299 | | /// in an inconsistent performance. |
| | | 300 | | /// </remarks> |
| | | 301 | | public static ValidationHelper ValidationRule<TViewModel, TViewModelProp>( |
| | | 302 | | this TViewModel viewModel, |
| | | 303 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 304 | | Func<TViewModel, IObservable<bool>> viewModelObservableProperty, |
| | | 305 | | Func<TViewModel, string> messageFunc) |
| | | 306 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 307 | | { |
| | 0 | 308 | | if (viewModel is null) |
| | | 309 | | { |
| | 0 | 310 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 311 | | } |
| | | 312 | | |
| | 0 | 313 | | if (viewModelProperty is null) |
| | | 314 | | { |
| | 0 | 315 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | if (viewModelObservableProperty is null) |
| | | 319 | | { |
| | 0 | 320 | | throw new ArgumentNullException(nameof(viewModelObservableProperty)); |
| | | 321 | | } |
| | | 322 | | |
| | 0 | 323 | | if (messageFunc is null) |
| | | 324 | | { |
| | 0 | 325 | | throw new ArgumentNullException(nameof(messageFunc)); |
| | | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | var validation = new ModelObservableValidation<TViewModel, TViewModelProp>( |
| | 0 | 329 | | viewModel, viewModelProperty, viewModelObservableProperty, messageFunc); |
| | | 330 | | |
| | 0 | 331 | | viewModel.ValidationContext.Add(validation); |
| | 0 | 332 | | return new ValidationHelper(validation); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Setup a validation rule with a general observable indicating validity. |
| | | 337 | | /// </summary> |
| | | 338 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 339 | | /// <typeparam name="TViewModelProp">ViewModel property type.</typeparam> |
| | | 340 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 341 | | /// <param name="viewModelProperty">ViewModel property referenced in viewModelObservableProperty.</param> |
| | | 342 | | /// <param name="viewModelObservableProperty">Func to define if the viewModel is valid or not.</param> |
| | | 343 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and viewModelOb |
| | | 344 | | /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| | | 345 | | /// <remarks> |
| | | 346 | | /// It should be noted that the observable should provide an initial value, otherwise that can result |
| | | 347 | | /// in an inconsistent performance. |
| | | 348 | | /// </remarks> |
| | | 349 | | [Obsolete("This overload is planned for future removal. Consider using either the overload that accepts a " + |
| | | 350 | | "Func<TViewModel, string> as the messageFunc parameter, or the overload that accepts a string.")] |
| | | 351 | | public static ValidationHelper ValidationRule<TViewModel, TViewModelProp>( |
| | | 352 | | this TViewModel viewModel, |
| | | 353 | | Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| | | 354 | | Func<TViewModel, IObservable<bool>> viewModelObservableProperty, |
| | | 355 | | Func<TViewModel, bool, string> messageFunc) |
| | | 356 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 357 | | { |
| | 4 | 358 | | if (viewModel is null) |
| | | 359 | | { |
| | 0 | 360 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 361 | | } |
| | | 362 | | |
| | 4 | 363 | | if (viewModelProperty is null) |
| | | 364 | | { |
| | 0 | 365 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 366 | | } |
| | | 367 | | |
| | 4 | 368 | | if (viewModelObservableProperty is null) |
| | | 369 | | { |
| | 0 | 370 | | throw new ArgumentNullException(nameof(viewModelObservableProperty)); |
| | | 371 | | } |
| | | 372 | | |
| | 4 | 373 | | if (messageFunc is null) |
| | | 374 | | { |
| | 0 | 375 | | throw new ArgumentNullException(nameof(messageFunc)); |
| | | 376 | | } |
| | | 377 | | |
| | 4 | 378 | | var validation = new ModelObservableValidation<TViewModel, TViewModelProp>( |
| | 4 | 379 | | viewModel, viewModelProperty, viewModelObservableProperty, messageFunc); |
| | | 380 | | |
| | 4 | 381 | | viewModel.ValidationContext.Add(validation); |
| | 4 | 382 | | return new ValidationHelper(validation); |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Gets an observable for the validity of the ViewModel. |
| | | 387 | | /// </summary> |
| | | 388 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 389 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 390 | | /// <returns>Returns true if the ValidationContext is valid, otherwise false.</returns> |
| | | 391 | | public static IObservable<bool> IsValid<TViewModel>(this TViewModel viewModel) |
| | | 392 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 393 | | { |
| | 2 | 394 | | if (viewModel == null) |
| | | 395 | | { |
| | 0 | 396 | | throw new ArgumentNullException(nameof(viewModel)); |
| | | 397 | | } |
| | | 398 | | |
| | 2 | 399 | | return viewModel.ValidationContext.Valid; |
| | | 400 | | } |
| | | 401 | | } |
| | | 402 | | } |