|
7 | 7 | using System.ComponentModel;
|
8 | 8 | using System.ComponentModel.DataAnnotations;
|
9 | 9 | using System.Linq;
|
| 10 | +using System.Text.RegularExpressions; |
10 | 11 | using Microsoft.Toolkit.Mvvm.ComponentModel;
|
11 | 12 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
12 | 13 |
|
@@ -355,6 +356,42 @@ public void Test_ObservableValidator_CustomValidation()
|
355 | 356 | Assert.AreEqual(events.Count, 0);
|
356 | 357 | }
|
357 | 358 |
|
| 359 | + [TestCategory("Mvvm")] |
| 360 | + [TestMethod] |
| 361 | + public void Test_ObservableValidator_CustomValidationWithInjectedService() |
| 362 | + { |
| 363 | + var model = new ValidationWithServiceModel(new FancyService()); |
| 364 | + var events = new List<DataErrorsChangedEventArgs>(); |
| 365 | + |
| 366 | + model.ErrorsChanged += (s, e) => events.Add(e); |
| 367 | + |
| 368 | + model.Name = "This is a valid name"; |
| 369 | + |
| 370 | + Assert.IsFalse(model.HasErrors); |
| 371 | + Assert.AreEqual(events.Count, 0); |
| 372 | + |
| 373 | + model.Name = "This is invalid238!!"; |
| 374 | + |
| 375 | + Assert.IsTrue(model.HasErrors); |
| 376 | + Assert.AreEqual(events.Count, 1); |
| 377 | + Assert.AreEqual(events[0].PropertyName, nameof(ValidationWithServiceModel.Name)); |
| 378 | + Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 1); |
| 379 | + |
| 380 | + model.Name = "This is valid but it is too long so the validation will fail anyway"; |
| 381 | + |
| 382 | + Assert.IsTrue(model.HasErrors); |
| 383 | + Assert.AreEqual(events.Count, 2); |
| 384 | + Assert.AreEqual(events[1].PropertyName, nameof(ValidationWithServiceModel.Name)); |
| 385 | + Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 1); |
| 386 | + |
| 387 | + model.Name = "This is both too long and it also contains invalid characters, a real disaster!"; |
| 388 | + |
| 389 | + Assert.IsTrue(model.HasErrors); |
| 390 | + Assert.AreEqual(events.Count, 3); |
| 391 | + Assert.AreEqual(events[2].PropertyName, nameof(ValidationWithServiceModel.Name)); |
| 392 | + Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 2); |
| 393 | + } |
| 394 | + |
358 | 395 | public class Person : ObservableValidator
|
359 | 396 | {
|
360 | 397 | private string name;
|
@@ -493,5 +530,54 @@ public static ValidationResult ValidateA(int x, ValidationContext context)
|
493 | 530 | return new ValidationResult("Missing the magic number");
|
494 | 531 | }
|
495 | 532 | }
|
| 533 | + |
| 534 | + public interface IFancyService |
| 535 | + { |
| 536 | + bool Validate(string name); |
| 537 | + } |
| 538 | + |
| 539 | + public class FancyService : IFancyService |
| 540 | + { |
| 541 | + public bool Validate(string name) |
| 542 | + { |
| 543 | + return Regex.IsMatch(name, @"^[A-Za-z ]+$"); |
| 544 | + } |
| 545 | + } |
| 546 | + |
| 547 | + /// <summary> |
| 548 | + /// Test model for custom validation with an injected service. |
| 549 | + /// See https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/3750 for the original request for this feature. |
| 550 | + /// </summary> |
| 551 | + public class ValidationWithServiceModel : ObservableValidator |
| 552 | + { |
| 553 | + private readonly IFancyService service; |
| 554 | + |
| 555 | + public ValidationWithServiceModel(IFancyService service) |
| 556 | + { |
| 557 | + this.service = service; |
| 558 | + } |
| 559 | + |
| 560 | + private string name; |
| 561 | + |
| 562 | + [MaxLength(25, ErrorMessage = "The name is too long")] |
| 563 | + [CustomValidation(typeof(ValidationWithServiceModel), nameof(ValidateName))] |
| 564 | + public string Name |
| 565 | + { |
| 566 | + get => this.name; |
| 567 | + set => SetProperty(ref this.name, value, true); |
| 568 | + } |
| 569 | + |
| 570 | + public static ValidationResult ValidateName(string name, ValidationContext context) |
| 571 | + { |
| 572 | + bool isValid = ((ValidationWithServiceModel)context.ObjectInstance).service.Validate(name); |
| 573 | + |
| 574 | + if (isValid) |
| 575 | + { |
| 576 | + return ValidationResult.Success; |
| 577 | + } |
| 578 | + |
| 579 | + return new ValidationResult("The name contains invalid characters"); |
| 580 | + } |
| 581 | + } |
496 | 582 | }
|
497 | 583 | }
|
0 commit comments