views:

56

answers:

2

Is it possible with NHibernate validators to get a validator that will validate one or other properties? In the example below either FirstName OR Surname is required.

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

    [OneOrOther("Group")]
    public string Surname {get; set; }
+2  A: 

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.

David
+2  A: 

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).

Jay