views:

185

answers:

0

If I have a class that holds phone numbers, called PhoneNumbers and I have a MVC view with two text entry boxes, each mapped to a PhoneNumber and I want one to be optional and one to be required, how can I achieve this with NHibernate Validator?

Right now my PhoneNumber class looks like this:

public class PhoneNumber
{
 [Pattern(@"^\d{3}$")]
 public virtual int AreaCode { get; set; }

 [Pattern(@"^\d{3}$")]
 public virtual int Prefix { get; set; }

 [Pattern(@"^\d{4}$")]
 public virtual int Suffix { get; set; }
}

And my form entity would look like this:

public class Form
{
 [Valid]
 public virtual PhoneNumber OfficeNumber { get; set; }

 [Valid]
 public virtual PhoneNumber FaxNumber { get; set; }
}

}

I want OfficeNumber to be required and FaxNumber to be optional, but valid if entered.

Also, if I'm using Digits, or Range, is there a way to limit their evaluation to only fire when a field contains data? Say I have a text entry box that accepts values within a particular range, but it's optional.

I'm just curious what the "Best Practices" are for these situations when using NHibernate Validators.

Thanks in advance, Dan