Created
May 13, 2013 16:09
-
-
Save hatelove/5569469 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GrandFather | |
{ | |
[StringLength(5, MinimumLength = 3)] | |
public string Name { get; set; } | |
[NestedValidation] | |
public List<Father> Fathers { get; set; } | |
} | |
public class Father | |
{ | |
[Range(1, 130)] | |
public int Age { get; set; } | |
[Required] | |
public string Name { get; set; } | |
[NestedValidation] | |
public IEnumerable<Son> Sons { get; set; } | |
} | |
public class Son : IValidatableObject | |
{ | |
[Range(1, 130)] | |
public int Age { get; set; } | |
[Required(AllowEmptyStrings = false)] | |
public string Name { get; set; } | |
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | |
{ | |
var result = new List<ValidationResult>(); | |
if (this.Age % 2 == 0) | |
{ | |
var memberNames = new List<string> { "Age" }; | |
result.Add(new ValidationResult("Age不得為偶數", memberNames)); | |
} | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void 驗證GrandFather() | |
{ | |
var grandFather = new GrandFather | |
{ | |
Name = "hi", | |
Fathers = new List<Father> | |
{ | |
new Father | |
{ | |
Name= "Father1", | |
Age=81, | |
Sons = new List<Son> | |
{ | |
new Son{Name="91", Age=300}, | |
new Son{Name = "92", Age= 11}, | |
new Son{Name = "93", Age= 202}, | |
} | |
}, | |
new Father | |
{ | |
Age=82, | |
Sons = new List<Son> | |
{ | |
new Son{Name="F2_1", Age=-1}, | |
new Son{Name = "F2_2", Age= -2}, | |
new Son{Name = "F2_3", Age= 4}, | |
} | |
}, | |
} | |
}; | |
var validator = new DataAnnotationValidator(); | |
var isValid = validator.TryValidate(grandFather); | |
Assert.AreEqual(false, isValid); | |
Assert.AreEqual(7, validator.ValidationResults.Count); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ValidationResults: | |
+ [0] {欄位 Name 必須是最小長度為 3 且最大長度為 5 的字串。} System.ComponentModel.DataAnnotations.ValidationResult | |
+ [1] {欄位 Fathers[0].Sons[0].Age 必須介於 1 到 130 之間。} System.ComponentModel.DataAnnotations.ValidationResult | |
+ [2] {欄位 Fathers[0].Sons[2].Age 必須介於 1 到 130 之間。} System.ComponentModel.DataAnnotations.ValidationResult | |
+ [3] {Fathers[1].Name 欄位是必要項。} System.ComponentModel.DataAnnotations.ValidationResult | |
+ [4] {欄位 Fathers[1].Sons[0].Age 必須介於 1 到 130 之間。} System.ComponentModel.DataAnnotations.ValidationResult | |
+ [5] {欄位 Fathers[1].Sons[1].Age 必須介於 1 到 130 之間。} System.ComponentModel.DataAnnotations.ValidationResult | |
+ [6] {Fathers[1].Sons[2].Age不得為偶數} System.ComponentModel.DataAnnotations.ValidationResult |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment