Question: I have the Review Model, I am trying to validate the model, so that
when the user selects a date, it can't be the date in the future.

Review.cs

```
public class Review : BaseEntity{

    [Key]
    public int Id {get; set;}

    [Required(ErrorMessage="You need a restaurant name!")]
    public string RestaurantName {get; set;}

    [What do I put in here??]
    public DateTime Date {get; set;}

}

```

I am a newbie, and the [documentation](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation) is kind of hard to understand.

Thank you so much for your help in advance.

Answer: 
You can create a custom validation attribute which does your custom logic and use that to decorate your property name.

```
public class DateLessThanOrEqualToToday : ValidationAttribute
{
    public override string FormatErrorMessage(string name)
    {
        return "Date value should not be a future date";
    }

    protected override ValidationResult IsValid(object objValue,
                                                   ValidationContext validationContext)
    {
        var dateValue = objValue as DateTime? ?? new DateTime();

        //alter this as needed. I am doing the date comparison if the value is not null

        if (dateValue.Date > DateTime.Now.Date)
        {
           return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
        return ValidationResult.Success;
    }
}

```

Now in your view model, decorate your property name with this new custom attribute

```
[DateLessThanOrEqualToToday]
public DateTime Date { get; set; }

```

This custom validation attribute is primarly focusing on your specific validation logic. You can alter it to include more null checks, minimum value check etc as needed.
Try **custom validation** or **standard model validation** with attributes.

First option, set attribute on property **standard dataannotations validation**:

* Set **DateType** attribute with errormessage properties and dateformatstring.
* Set **Range** attribute if you want.
* Set **Display** attribute for show label on screen.

```
[DataType(DataType.Date), ErrorMessage = "Please enter a correct date format dd/mm/yyyy hh:mm", DisplayFormat( DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode=true )]
[Range(typeof(DateTime), "1/1/2016", "1/1/2011")]
[Display(Name = "My Date")]
public DateTime Date {get; set;}

```

Second option, **Custom validation method**:

You must extend the **ValidationAttribute** class and override `IsValid`:

```
public class MyDateValidation: ValidationAttribute
{
    public override bool IsValid(object value)
    {
        // check your business date property  
        DateTime myDatetime;
        bool isParsed = DateTime.TryParse((string)value, out myDatetime);
        if(!isParsed)
            return false;
        return true;
    }
}

[MyDateValidation(ErrorMessage="Your message")]
public Datetime myDate { get; set; }

```

See my other [answer](https://stackoverflow.com/questions/39608083/custom-dataannotation-isvalid-not-called/39608988#39608988) about this subject.