0

I would like to know how to set a conditional requirement on a DateTime property. That is, in addition to check whether or not this required field is empty, I want the input (in a cshtml file) to be no sooner than 3 weeks from today.

Model:

[DataType(DataType.Date)]
[Display(Name = "Start date"), Required(ErrorMessage = ValidationMessages.IsRequired)]
//[What else here for this condition??]
public DateTime StartDate { get; set; }

.cshtml:

<div class="form-group">
    <div class="editor-label">
        @Html.LabelFor(model => model.Assignment.StartDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Assignment.StartDate)
        @Html.ValidationMessageFor(model => model.Assignment.StartDate)
    </div>
</div>

How would such a conditional expression look like? Need I add something besides the condition in the model?

And please say if my description is too scanty.

// Thanks in advance, Regards

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
user3147607
  • 119
  • 1
  • 2
  • 11
  • Look this post http://stackoverflow.com/questions/1406046/data-annotation-ranges-of-dates – Suni Jan 30 '14 at 20:30

2 Answers2

0

You can do this in your model. Add proper Error Messages.

[Required(ErrorMessage = "")]

[Range(typeof(DateTime), DateTime.Now.ToString(), DateTime.Now.AddDays(21).ToString(), ErrorMessage = "" )]

public DateTime StartDate { get; set; }
L.B
  • 114,136
  • 19
  • 178
  • 224
sanksk
  • 13
  • 3
  • 1
    Correct me if I'm wrong, but won't this generate a compile error: "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"? – LiquidPony Jan 30 '14 at 20:40
  • I will try that. Cannot try until tomorrow though. A question, this looks like it has to be between today and three weeks ahead, am I wrong? – user3147607 Jan 30 '14 at 20:47
0

You can create your own validation attribute like below:

1) Custom validation attribute with custom error message

public class CheckInputDateAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var inputDate = (DateTime)value;
        var compareDate = DateTime.Now.AddDays(21);
        int result = DateTime.Compare(inputDate, compareDate);
        const string sErrorMessage = "Input date must be no sooner than 3 weeks from today.";
        if (result < 0)
        {
            return new ValidationResult(sErrorMessage);
        }
        return ValidationResult.Success;
    }
}

Then use it like

  [DataType(DataType.Date)]
  [CheckInputDate]
  public DateTime StartDate { get; set; }

2) Custom validation attribute without custom error message

public class CheckInputDateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var inputDate = (DateTime)value;
        var compareDate = DateTime.Now.AddDays(21);
        int result = DateTime.Compare(inputDate, compareDate);
        return result >= 0;
    }
}

Then use it like

    [DataType(DataType.Date)]
    [Display(Name = "Start date")]   
    [CheckInputDate]
    public DateTime StartDate { get; set; }
Lin
  • 15,078
  • 4
  • 47
  • 49
  • Thanks, this works nicely. However, and I feel a bit dumb for not figuring this out. Instead of show the the property name and the message: "[Assignment.StartDate] = Input date must be no sooner than 3 weeks from today.", I would like to show the Display Name and the error message. – user3147607 Jan 31 '14 at 07:22
  • Hi, sorry for late reply. But your updated answer fixed it. Thank you, this post can be set to 'solved'. – user3147607 Jan 31 '14 at 21:54