Multiple MVC Foolproof Validation

351 Views Asked by At

I am using MVC Foolproof Validation for my MVC 5 app.

I'm trying to say: EventPlanEnd is not required unless EventPlanStart is filled in. If it is, make sure EventPlanEnd > EventPlanStart. This doesn't seem to work...

 public Nullable<System.DateTime> EventPlanStart { get; set; }

 [RequiredIfTrue("EventPlanStart")]
 [GreaterThan("EventPlanStart")]
 public Nullable<System.DateTime> EventPlanEnd { get; set; }

Any ideas how I can do this with data annotations?

1

There are 1 best solutions below

0
rageit On

Maybe you can use RequiredIfNot instead of RequiredIfTrue which takes a boolean. This is an untested code:

public Nullable<System.DateTime> EventPlanStart { get; set; }

[RequiredIfNot("EventPlanStart", null)]
[GreaterThan("EventPlanStart")]
public Nullable<System.DateTime> EventPlanEnd { get; set; }