我一直對抗這個問題,我本來以爲可能是多態/繼承做影響了我的驗證,但香港專業教育學院也縮小到了這方面的工作......驗證應用程序塊的對象驗證不規則集
這裏是階級結構..
public class Employee {
[ObjectValidator(Ruleset = "A")]
public EmployeeName Name { get; set; }
public Employee()
{
Name = new EmployeeName();
}
}
public class EmployeeName
{
[StringLengthValidator(1,20,Ruleset = "A")]
public string First { get; set; }
public string Last { get; set; }
public EmployeeName()
{
First = string.Empty;
Last = string.Empty;
}
}
測試:
[TestFixture]
public class ObjectValidationWithRulesets
{
[Test]
public void wont_validate_with_a_ruleset()
{
var person = new Employee()
{
Name = new EmployeeName()
{
First = string.Empty,
Last = string.Empty
}
};
var ruleSetValidator =
ValidationFactory.CreateValidator<Employee>("A");
var validationResults = ruleSetValidator.Validate(person);
Assert.That(!validationResults.IsValid,
"Validation with rulsets failed");
}
}
該測試通過,如果我刪除刪除規則集的東西。但是一旦應用了規則集,我就不能讓對象正確驗證。
任何人都可以對此有所瞭解嗎?
乾杯,
確定。 因此,我一直在使用web.config文件中的驗證配置,事實證明,要使ObjectValidator使用規則集,它需要定義其targetRuleSet屬性。 有趣的是,這個字段不暴露在ObjectValidatorAttribute上。 這是設計嗎? – boz 2009-11-19 22:59:01