0

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?

WebDevGuy2
  • 1,159
  • 1
  • 19
  • 40
  • Possible duplicate of [MVC Foolproof Validation using PassOnNull - Nullable or "01.01.0001 00:00:00"](http://stackoverflow.com/questions/12406297/mvc-foolproof-validation-using-passonnull-nullable-or-01-01-0001-000000) – CodeCaster Mar 01 '17 at 16:00
  • `[RequiredIfTrue]` is only applicable if the other property is a `bool` –  Mar 01 '17 at 21:55

1 Answers1

0

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; }
rageit
  • 3,513
  • 1
  • 26
  • 38