0

I am able to validate all my fields individually in my form but as soon as i have to compare two field together, it's impossible for me to do it.

Let's say i wanna check if StartingDate is smaller than EndingDate , how is it possible for me to output an ErrorMEssage if it's not valid.

I tried with a Remote tag to call a function that check both datas but it's not working because only the field who call the function has data, it's weird.

Any idea ? Thanks a lot.

public class Hospitalisation
    {
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        [DataType(DataType.Date)]
        [Display(Name = "Starting Date")]
        public string StartDate { get; set; } = string.Empty;

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        [DataType(DataType.Date)]
        [Display(Name = "Ending Date")]
        public string EndDate { get; set; } = string.Empty;
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Does this answer your question? [Custom Validation Attributes: Comparing two properties in the same model](https://stackoverflow.com/questions/41900485/custom-validation-attributes-comparing-two-properties-in-the-same-model) – gunr2171 Aug 25 '21 at 19:21
  • @gunr2171 Ty for answer, not really i implemented the solution and somehow the EndDate is not in the validationContext.ObjectInstance . ( Same problem i had using remote) I am still baffled at how hard it is to do this in MVC haha –  Aug 25 '21 at 20:09
  • @gunr2171 so i applied the answer from rschoenbach and it worked but it's only validated on submit which i guess is better than nothing.. –  Aug 25 '21 at 20:36

0 Answers0