0

Is it possible to conditionally validate a field with the server side validation helpers?

E.G.

public partial class SomeEntity
{
   public bool Chargeable { get; set; }
   [RegularExpression(@"\d{8,8}", ErrorMessage = "should have alpha numeric characters.")]
   public string CaseNumber { get; set; }
}

Requirement: CaseNumber is required if and only if Chargeable is true.

I could do:

 [RegularExpression(@"\d{8,8}", ErrorMessage = "should have alpha numeric characters."), Required]
 public string CaseNumber { get; set; }

However, Required(if(Chargeable)) is what I need...

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • possible duplicate of [RequiredIf Conditional Validation Attribute](http://stackoverflow.com/questions/7390902/requiredif-conditional-validation-attribute) – jbabey Jun 06 '13 at 16:55

1 Answers1

3

Make your SomeEntity class implement the IValidatableObject interface.

serge.karalenka
  • 980
  • 1
  • 15
  • 29