Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc validate date/time is at least 1 minute in the future

I am still getting my hands around MVC.

I have seen several similar questions, some custom code and various methods but I have not found something that works for me.

I have a search model that fills an HTML table with results inside of a partial view. I have this in my search results model:

public DateTime? BeginDateTime { get; set; }

Which is set to DateTime.Now in the controller. The user can specify that date and time to run a task with the search results' data on the model's POST call.

What I would like to do is validate that the date/time the user defined is at least 1 minute in the future. If this can be done as a client-side validation it will be better, but I am open to options as long as it works.

View:

Begin update: @Html.TextBoxFor(o => o.BeginDateTime, new { id="txtBegin" })

Thanks.

like image 981
user1970778 Avatar asked Feb 18 '13 21:02

user1970778


1 Answers

Create a new Attribute:

public class FutureDateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return value != null && (DateTime)value > DateTime.Now;
    }
}

Now in your model set this attribute:

[FutureDate(ErrorMessage="Date should be in the future.")]
public DateTime Deadline { get; set; }
like image 159
amaters Avatar answered Nov 15 '22 20:11

amaters