user565992
user565992

Reputation: 497

ASP.NET core MVC custom validation compare dates

I have two date on my razor view and I need to valdiate on that and using data annotation show the error if To date is less than from date. when I run the code in the debug mode the break point hits but it is not rendering the error on the page. it displays the form again the same way if the model state is not valid Below is my code, am I doing something wrong.

public class AcctViewModel: IValidatableObject
{

 [Key] public long AccountId { get; set;}

  [Display(Name = "Account Name"), DataType(DataType.Text), StringLength(15), Required]
  public string AcctName {get;set;}
   
   [Display(Name = "From Date"), DataType(DataType.Date), Required]
   public DateTime FromDate { get; set; }

   [Display(Name = "To Date"), DataType(DataType.Date), Required]
   public DateTime ToDate { get; set; }


   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> errors = new List<ValidationResult>();
            if (ToDate < FromDate)
            {
                errors.Add(new ValidationResult($"{nameof(ToDate)} needs to be greater than From date.", new List<string> { nameof(ToDate) }));
            }
            return errors;
        }

}

 <div class="form-row">
                        <div class="col-md-12">
                            <div class="form-group form-group--float">
                                <input class="form-control date-picker" asp-for="ToDate">
                                <label asp-for="ToDate"></label>
                              
                                <div class="invalid-tooltip"><span asp-validation-for="ToDate"></span></div>
                                <i class="form-group__bar"></i>
                            </div>
                        </div>
                    </div>

                </div>

Upvotes: 0

Views: 2313

Answers (1)

foad abdollahi
foad abdollahi

Reputation: 1978

Controller:

 public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Index(AcctViewModel model)
        {
            if (ModelState.IsValid)
            {

            }
            return View(model);
        }

View:

   <form asp-action="Index">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="AccountId" class="control-label"></label>
                <input asp-for="AccountId" class="form-control" />
                <span asp-validation-for="AccountId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="AcctName" class="control-label"></label>
                <input asp-for="AcctName" class="form-control" />
                <span asp-validation-for="AcctName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FromDate" class="control-label"></label>
                <input asp-for="FromDate" class="form-control" />
                <span asp-validation-for="FromDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ToDate" class="control-label"></label>
                <input asp-for="ToDate" class="form-control" />
                <span asp-validation-for="ToDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>

model:

   public class AcctViewModel : IValidatableObject
    {
        [Key] 
        public long AccountId { get; set; }

        [Display(Name = "Account Name"), DataType(DataType.Text), StringLength(15), Required]
        public string AcctName { get; set; }

        [Display(Name = "From Date"), DataType(DataType.Date), Required]
        public DateTime FromDate { get; set; }

        [Display(Name = "To Date"), DataType(DataType.Date), Required]
        public DateTime ToDate { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> errors = new List<ValidationResult>();
            if (ToDate < FromDate)
            {
                errors.Add(new ValidationResult($"{nameof(ToDate)} needs to be greater than From date.", new List<string> { nameof(ToDate) }));
            }
            return errors;
        }
    }

Upvotes: 1

Related Questions