开始日期应小于 ASP.Net 核心 MVC 中的结束日期验证

Start Date should be less than End date validation in ASP.Net Core MVC

我正在使用 .net 核心创建 TermStartDateEndDate,其中包括接收 [=14= 报告的 ClosureDate ].这里我想要一个 StartDate> ClosureDate 和一个 EndDate<ClosureDate。当然还有 StartDate<Enddate。那么在这里我应该如何测试呢?在我的 Repository 中处理它们或使用 scriptsView 中处理它们? 这是我的代码:

存储库

 public bool CreateSemester(Semester semester)
    {
        if (CheckExistSemester(semester.SemesterName))
        {
            return false;
        }
        var newSemester = new Semester
        {
            SemesterName = semester.SemesterName,
            SemesterStartDate = semester.SemesterStartDate,
            SemesterEndDate = semester.SemesterEndDate,
            ClosureDate = semester.ClosureDate
        };
        _dbContext.Semesters.Add(newSemester);
        _dbContext.SaveChanges();
        return true;
    }

您可以进行自定义验证。

创建 CustomModelValidatorProvider class。

 public class CustomModelValidatorProvider : IModelValidatorProvider
{

    public void CreateValidators(ModelValidatorProviderContext context)
    {
        if (context.Results.Any(v => v.Validator.GetType() == typeof(MyCustomModelValidator)) == true)
        {
            return;
        }

        if (context.ModelMetadata.ContainerType == typeof(Semester))
        {
            context.Results.Add(new ValidatorItem
            {
                Validator = new MyCustomModelValidator(),
                IsReusable = true
            });
        }
    }
}

public class MyCustomModelValidator : IModelValidator
{
    public IEnumerable<ModelValidationResult> Validate(ModelValidationContext context)
    {
        var model = context.Container as Semester;

       
        if (context.ModelMetadata.ModelType == typeof(DateTime)
            && context.ModelMetadata.Name == nameof(model.SemesterStartDate))
            { 
      
        if (model.StartDate > model.ClosureDate)
        {
            return new List<ModelValidationResult>
                {
                   new ModelValidationResult("","Start Date should be less than Closure date")
                };
            }
        }
        if (context.ModelMetadata.ModelType == typeof(DateTime)
            && context.ModelMetadata.Name == nameof(model.SemesterEndDate))
        {

            if (model.EndDate < model.ClosureDate)
            {
                return new List<ModelValidationResult>
                {
                   new ModelValidationResult("","End Date should be grater than Closure date")
                };
            }
        }
      
        return Enumerable.Empty<ModelValidationResult>();
    }
}
}

在你的 Startup

 services.AddControllersWithViews(opt => opt.ModelValidatorProviders.Add(new CustomModelValidatorProvider()));

测试结果:

您可以使用 Fluent Validation。自定义规则更灵活

以下是您要实现的流畅验证示例。

public class SearchValidator: AbstractValidator
{
  public SearchValidator()
  {
    RuleFor(m => m.StartDate)
        .NotEmpty()
        .WithMessage("Start Date is Required");

    RuleFor(m => m.EndDate)
        .NotEmpty().WithMessage("End date is required")
        .GreaterThan(m => m.StartDate.Value)
                        .WithMessage("End date must after Start date")
        .When(m => m.StartDate.HasValue);
  }
}

here 是一篇解释清楚的文章,会对您有所帮助。

here是流利验证的文档