|
8 | 8 | using System.Linq;
|
9 | 9 | using System.Linq.Expressions;
|
10 | 10 | using System.Reactive.Disposables;
|
| 11 | +using System.Reactive.Linq; |
11 | 12 | using ReactiveUI.Validation.Abstractions;
|
12 | 13 | using ReactiveUI.Validation.Components;
|
13 | 14 | using ReactiveUI.Validation.Components.Abstractions;
|
@@ -227,6 +228,39 @@ public static ValidationHelper ValidationRule<TViewModel>(
|
227 | 228 | validationObservable));
|
228 | 229 | }
|
229 | 230 |
|
| 231 | + /// <summary> |
| 232 | + /// Setup a validation rule with a general observable based on <see cref="IValidationState"/>. |
| 233 | + /// </summary> |
| 234 | + /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| 235 | + /// <typeparam name="TValue">Validation observable type.</typeparam> |
| 236 | + /// <param name="viewModel">ViewModel instance.</param> |
| 237 | + /// <param name="validationObservable">Observable to define if the viewModel is valid or not.</param> |
| 238 | + /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| 239 | + /// <remarks> |
| 240 | + /// It should be noted that the observable should provide an initial value, otherwise that can result |
| 241 | + /// in an inconsistent performance. |
| 242 | + /// </remarks> |
| 243 | + public static ValidationHelper ValidationRule<TViewModel, TValue>( |
| 244 | + this TViewModel viewModel, |
| 245 | + IObservable<TValue> validationObservable) |
| 246 | + where TViewModel : IReactiveObject, IValidatableViewModel |
| 247 | + where TValue : IValidationState |
| 248 | + { |
| 249 | + if (viewModel is null) |
| 250 | + { |
| 251 | + throw new ArgumentNullException(nameof(viewModel)); |
| 252 | + } |
| 253 | + |
| 254 | + if (validationObservable is null) |
| 255 | + { |
| 256 | + throw new ArgumentNullException(nameof(validationObservable)); |
| 257 | + } |
| 258 | + |
| 259 | + return viewModel.RegisterValidation( |
| 260 | + new ObservableValidation<TViewModel, bool>( |
| 261 | + validationObservable.Select(s => s as IValidationState))); |
| 262 | + } |
| 263 | + |
230 | 264 | /// <summary>
|
231 | 265 | /// Setup a validation rule with a general observable indicating validity and a static error message
|
232 | 266 | /// for the given view model property.
|
@@ -372,6 +406,47 @@ public static ValidationHelper ValidationRule<TViewModel, TViewModelProp>(
|
372 | 406 | viewModelProperty, validationObservable));
|
373 | 407 | }
|
374 | 408 |
|
| 409 | + /// <summary> |
| 410 | + /// Setup a validation rule with a general observable based on <see cref="IValidationState"/>. |
| 411 | + /// </summary> |
| 412 | + /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| 413 | + /// <typeparam name="TViewModelProp">ViewModel property type.</typeparam> |
| 414 | + /// <typeparam name="TValue">Validation observable type.</typeparam> |
| 415 | + /// <param name="viewModel">ViewModel instance.</param> |
| 416 | + /// <param name="viewModelProperty">ViewModel property referenced in viewModelObservableProperty.</param> |
| 417 | + /// <param name="validationObservable">Observable to define if the viewModel is valid or not.</param> |
| 418 | + /// <returns>Returns a <see cref="ValidationHelper"/> object.</returns> |
| 419 | + /// <remarks> |
| 420 | + /// It should be noted that the observable should provide an initial value, otherwise that can result |
| 421 | + /// in an inconsistent performance. |
| 422 | + /// </remarks> |
| 423 | + public static ValidationHelper ValidationRule<TViewModel, TViewModelProp, TValue>( |
| 424 | + this TViewModel viewModel, |
| 425 | + Expression<Func<TViewModel, TViewModelProp>> viewModelProperty, |
| 426 | + IObservable<TValue> validationObservable) |
| 427 | + where TViewModel : IReactiveObject, IValidatableViewModel |
| 428 | + where TValue : IValidationState |
| 429 | + { |
| 430 | + if (viewModel is null) |
| 431 | + { |
| 432 | + throw new ArgumentNullException(nameof(viewModel)); |
| 433 | + } |
| 434 | + |
| 435 | + if (viewModelProperty is null) |
| 436 | + { |
| 437 | + throw new ArgumentNullException(nameof(viewModelProperty)); |
| 438 | + } |
| 439 | + |
| 440 | + if (validationObservable is null) |
| 441 | + { |
| 442 | + throw new ArgumentNullException(nameof(validationObservable)); |
| 443 | + } |
| 444 | + |
| 445 | + return viewModel.RegisterValidation( |
| 446 | + new ObservableValidation<TViewModel, bool, TViewModelProp>( |
| 447 | + viewModelProperty, validationObservable.Select(v => v as IValidationState))); |
| 448 | + } |
| 449 | + |
375 | 450 | /// <summary>
|
376 | 451 | /// Clears the validation rules associated with teh specified property.
|
377 | 452 | /// </summary>
|
|
0 commit comments