P.Brian.Mackey
P.Brian.Mackey

Reputation: 44295

Perform conditional validation

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

Upvotes: 0

Views: 61

Answers (1)

serge.karalenka
serge.karalenka

Reputation: 990

Make your SomeEntity class implement the IValidatableObject interface.

Upvotes: 3

Related Questions