0

I am trying to create a custom attribute in my MVC 5 web application to validate that a DateTime field falls within 30 to 50 days of the current date. If the date does not fall between the 30 to 50 day window, an error is thrown. I tried using this post to build my attribute, but I'm not sure where I'm going wrong with my code. Currently, this code builds but nothing happens when I enter an invalid date.

public class CustomDateAttribute : RangeAttribute
{
    public CustomDateAttribute()
      : base(typeof(DateTime),
          DateTime.Now.AddDays(30).ToShortDateString(),
          DateTime.Now.AddDays(50).ToShortDateString()
        )
    { }
}
cryan
  • 119
  • 1
  • 13
  • 1
    Have you registered it in `global.asax.cs`? –  Mar 26 '18 at 20:54
  • I just registered it, thank you. I'm now running into an issue when I run the code. No matter if the date is within the 30-50 day range, I keep getting an error saying "The date must be between x and y." I have tried entering a date in the range, the min/max dates, dates outside of the range and the error won't go away. When I clear the textbox, though, the error disappears as expected. Any ideas? – cryan Mar 27 '18 at 13:27
  • I'm guessing your mean you are getting a client side error - you would need to reconfigure the `$.validator` for a date range (and no idea why you accepted that answer - why write all that extra code that reduces functonality) –  Mar 27 '18 at 20:22
  • Wow, I just figured out how to fix the client side error you were telling me about with the code you linked to in my other post. Yes, your solution was much more effective (although I'm still wrapping my head around what I did). Thank you. – cryan Mar 28 '18 at 15:36

1 Answers1

1

This should do what you're looking for. You don't necessarily need to inherit from the RangeAttribute class to get the functionality you need.

[AttributeUsage(AttributeTargets.Property)]
public class CustomDateAttribute : ValidationAttribute
{
  public CustomDateAttribute(string errorMessage) : base(errorMessage) { }

  public override bool IsValid(object value)
  {
    if (value == null) return false;

    DateTime? dateTime = value as DateTime?;

    if (dateTime.HasValue)
    {
      return dateTime.Value >= DateTime.Now.AddDays(30) && dateTime.Value <= DateTime.Now.AddDays(50);
    }

    return false;
  }
}
JB06
  • 1,881
  • 14
  • 28
  • I'm getting an error with "value as DateTime" that is "The 'as' operator must be used with a reference or nullable type (DateTime is non-nullable)" Any ideas on how to troubleshoot this? – cryan Mar 27 '18 at 13:31
  • You can either change `value as DateTime` to `Convert.ToDateTime(value)` or change the variable `dateTime` to nullable. – JB06 Mar 27 '18 at 17:56
  • Ran into a new error. My attribute tag, [CustomDate] now has the error "no argument given to the required formal parameter errorMessage." Any ideas about this one? Sorry, this is my first time doing this. I feel so inept. – cryan Mar 27 '18 at 18:52
  • Yeah, you just have to give it the error message you want it to show if its an invalid date. So `[CustomDate("Date is invalid")]`. You could add a parameterless constructor and set a default in that if you don't want to specify the error message every time. – JB06 Mar 27 '18 at 18:56
  • OH WOW. I feel very dumb. I was trying to do it as [CustomDate(ErrorMessage = "this is an error message")]. No wonder it wasn't working. Thank you very much! – cryan Mar 27 '18 at 19:03