views:

32

answers:

2

I need to validate two fields only if a third field has a specific value. In this code snipper i suppose to use a CheckIf properties that not exist. It is possible to validate a field only if another property hase a specifica value ?

public string CustomerType { get; set; } // P=Private B=Business

[NotNullValidator(MessageTemplate = "You must specify the property 'Name'", CheckIf = "CustomerType=='P'")]
public string PrivateName { get; set; }

[NotNullValidator(MessageTemplate = "You must specify the property 'Name'", CheckIf = "CustomerType=='B'")]
public string BusinessName { get; set; }

Thank you!!!

+2  A: 

You may consider using : SelfValidation . For more information you please visit :

http://davidhayden.com/blog/dave/archive/2007/01/24/BusinessObjectValidationEnterpriseLibrary.aspx

Siva Gopal
A: 

From a validation perspective I agree with Siva that you can use SelfValidation for this. When looking at your code however, from an OO perspective, I can't help noticing that it might be good to take a good look at your design. It seems that either you are showing us two sub types of Customer, namely PrivateCustomer and BusinessCustomer:

class Customer
{
}

class PrivateCustomer : Customer
{
    public string PrivateName { get; set; }
}

class BusinessCustomer : Customer
{
    public string BusinessName { get; set; }
}

Or... those two properties are actually the same thing. Your validation messages even calls them 'Name' in both cases. In that case, you'll end up with this design:

class Customer : Customer
{
    public string CustomerType { get; set; }

    public string Name { get; set; }
}
Steven
My situation is similar to the second. When the customer type is "B"=Business the "Private Name" must be the name of the Legal and the "Business Name" is the name of the company. I've used SefValidation but I think that for the readbility of the code an attribute that link a validation to a value of a property will be useful. Thank you!
Cyberman