0

I have a playlist Model class in MVC4 that has a Date property, and when a user creates it, I want them not to be able to specify a date before today (so today or after).

Here's part of my Playlist class:

[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        [Required(ErrorMessage = "Incorrect date format.")]
        public DateTime Date { get; set; }

How do I add validation for this "range"? Right now it's only validating to see if it's a valid date, but 11/30/2012 is valid and won't throw up any errors even though it was yesterday.

I'm using Razor for my Views. Thank you.

swiftcode
  • 3,039
  • 9
  • 39
  • 64

1 Answers1

1

You can't validate against dynamic values using the Range attribute. You can create your own validator based on ValidationAttribute that validates the date against the current or a calculated date. Or you can use the IValidatableObject interface in your model class (buddy class to stop it getting overwritten by EF), and validate the properties you want there.

Paul Taylor
  • 5,651
  • 5
  • 44
  • 68