开发者

NHibernate Validator One Value OR Another

Is it possible with NHibernate validators to get a validator that will validate one or other properties? In the example below either Firs开发者_StackOverflow中文版tName OR Surname is required.

    [OneOrOther("Group")]
    public string FirstName {get; set; }

    [OneOrOther("Group")]
    public string Surname {get; set; }


I think this post (http://devlicio.us/blogs/billy_mccafferty/archive/2009/07/30/writing-a-custom-nhibernate-class-validator.aspx) can be applied nicely in this case. I think the only difference is that the sample uses a mutually exclusive condition (one or the other, but not both -- XOR).


Yes you can create a property on the object to test whether the condition holds true and then use one of the nhibernate decorators to check that it is valid i.e. true.

From the documentation:

AssertTrue property check that the method evaluates to true (useful for constraints expressed in code rather than annotations)

This is by far the simplest method to implement the particular problem you have described, you can create a custom class level validator but hey its maybe more work than you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜