@@ -392,6 +392,28 @@ public void Test_ObservableValidator_CustomValidationWithInjectedService()
392
392
Assert . AreEqual ( model . GetErrors ( nameof ( ValidationWithServiceModel . Name ) ) . ToArray ( ) . Length , 2 ) ;
393
393
}
394
394
395
+ [ TestCategory ( "Mvvm" ) ]
396
+ [ TestMethod ]
397
+ public void Test_ObservableValidator_ValidationWithFormattedDisplayName ( )
398
+ {
399
+ var model = new ValidationWithDisplayName ( ) ;
400
+
401
+ Assert . IsTrue ( model . HasErrors ) ;
402
+
403
+ // We need to order because there is no guaranteed order on the members of a type
404
+ ValidationResult [ ] allErrors = model . GetErrors ( ) . OrderBy ( error => error . ErrorMessage ) . ToArray ( ) ;
405
+
406
+ Assert . AreEqual ( allErrors . Length , 2 ) ;
407
+
408
+ Assert . AreEqual ( allErrors [ 0 ] . MemberNames . Count ( ) , 1 ) ;
409
+ Assert . AreEqual ( allErrors [ 0 ] . MemberNames . Single ( ) , nameof ( ValidationWithDisplayName . StringMayNotBeEmpty ) ) ;
410
+ Assert . AreEqual ( allErrors [ 0 ] . ErrorMessage , $ "FIRST: { nameof ( ValidationWithDisplayName . StringMayNotBeEmpty ) } .") ;
411
+
412
+ Assert . AreEqual ( allErrors [ 1 ] . MemberNames . Count ( ) , 1 ) ;
413
+ Assert . AreEqual ( allErrors [ 1 ] . MemberNames . Single ( ) , nameof ( ValidationWithDisplayName . AnotherRequiredField ) ) ;
414
+ Assert . AreEqual ( allErrors [ 1 ] . ErrorMessage , $ "SECOND: { nameof ( ValidationWithDisplayName . AnotherRequiredField ) } .") ;
415
+ }
416
+
395
417
public class Person : ObservableValidator
396
418
{
397
419
private string name ;
@@ -579,5 +601,35 @@ public static ValidationResult ValidateName(string name, ValidationContext conte
579
601
return new ValidationResult ( "The name contains invalid characters" ) ;
580
602
}
581
603
}
604
+
605
+ /// <summary>
606
+ /// Test model for validation with a formatted display name string on each property.
607
+ /// This is issue #1 from https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/3763.
608
+ /// </summary>
609
+ public class ValidationWithDisplayName : ObservableValidator
610
+ {
611
+ public ValidationWithDisplayName ( )
612
+ {
613
+ ValidateAllProperties ( ) ;
614
+ }
615
+
616
+ private string stringMayNotBeEmpty ;
617
+
618
+ [ Required ( AllowEmptyStrings = false , ErrorMessage = "FIRST: {0}." ) ]
619
+ public string StringMayNotBeEmpty
620
+ {
621
+ get => this . stringMayNotBeEmpty ;
622
+ set => SetProperty ( ref this . stringMayNotBeEmpty , value , true ) ;
623
+ }
624
+
625
+ private string anotherRequiredField ;
626
+
627
+ [ Required ( AllowEmptyStrings = false , ErrorMessage = "SECOND: {0}." ) ]
628
+ public string AnotherRequiredField
629
+ {
630
+ get => this . anotherRequiredField ;
631
+ set => SetProperty ( ref this . anotherRequiredField , value , true ) ;
632
+ }
633
+ }
582
634
}
583
635
}
0 commit comments