Search code examples
c#asp.netasp.net-mvc-4data-annotations

Is there a way through data annotations to verify that one date property is greater than or equal to another date property?


I have a StartDate and EndDate on my SchoolEvents Model and I was wondering if there are any data annotations I could use to verify that the StartDate is less than or equal to the EndDate and that the EndDate is greater than or equal to the StartDate?


Solution

  • From my point of view, you have to build a custom validation attribute. You can look at the link to validate follow specific your validation. It will take your efforts so much. Instead of you use data annotation you should apply Fluent Validation which will help you reduce efforts. It is easy to setup, straight forward and separates of concern, you do not need mixing between view models, domain objects, and validations which depend on business rule. Sample code using the CustomValidation in System.ComponentModel.DataAnnotations

    [CustomValidation(typeof(StartDateDateValidation), "ValidateStartDate")]
    public class SampleModel
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
        
        public DateTime StartDate { get; set; }
    
        public DateTime EndDate { get; set; }
    }
    
        public class StartDateDateValidation
        {
            public static ValidationResult ValidateStartDate(SampleModel sampleModel)
            {
            bool isValid = sampleModel.StartDate <= sampleModel.EndDate;
    
            if (isValid)
            {
                return ValidationResult.Success;
            }
            
            return new ValidationResult(
                "The start date value must be smaller or equal the end date value. ");          
       }
    

    Using Validator class in System.ComponentModel.DataAnnotations to validate the object values:

    SampleModel model = new SampleModel
    {
         Id = 1,
         Name = "Test",
         StartDate = new DateTime(1990, 1, 1),
         EndDate = new DateTime(1992, 1, 1)
    };
    
    Validator.ValidateObject(model, new ValidationContext(model));
    

    Using FluentValidation sample:

    public class FluentSampleModel
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
        
        public DateTime StartDate { get; set; }
    
        public DateTime EndDate { get; set; }
    }
    
    public class FluentSampleModelValidator : AbstractValidator<FluentSampleModel>
    {
        public FluentSampleModelValidator()
        {
            RuleFor(x => x.StartDate <= x.EndDate);
        }
    }
    

    the sample for validation:

    FluentSampleModel fluentModel = new FluentSampleModel()
    {
        Id = 1,
        Name = "Test",
        StartDate = new DateTime(1990, 1, 1),
        EndDate = new DateTime(1992, 1, 1)
    };
    
    FluentSampleModelValidator validator = new FluentSampleModelValidator();
    var result = validator.Validate(fluentModel);
    
    if (result != null && result.IsValid)
    {
        // Handle valid data model
    }