asp.net 自定义验证属性不显示错误消息

e3bfsja2  于 2023-02-14  发布在  .NET
关注(0)|答案(1)|浏览(226)

特灵比较www.example.com核心中的两个日期输入asp.net,我没有收到任何错误消息。
其他内置验证器正在工作。也尝试创建自定义验证器。
(使用元组模型.并将其命名为“e”)
我的模型:

public DateTime? SchoolStart { get; set; }
    public DateTime? SchoolEnd { get; set; }

模型验证器(可验证对象)

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (SchoolEnd.Value <= SchoolStart.Value)
            {
                yield return new ValidationResult("End date must be greater than the start date.", new[] { nameof(SchoolEnd) });
            }
        }

我的看法

<input asp-for="e.SchoolStart" type="date" class="form-control" data-provider="flatpickr" data-date-format="d F Y" placeholder="Başlangıç Tarihi" />

<input asp-for="e.SchoolEnd" type="date" class="form-control" data-provider="flatpickr" data-date-format="d F Y" placeholder="Bitiş Tarihi" />
6tdlim6h

6tdlim6h1#

这是后端验证,ModelState.IsValid后将显示错误消息,请参考此简单演示:

    • 型号**
public class DataModel
    {
        [Required]
        public string Test { get; set; }
        public DateTime? SchoolStart { get; set; }

        [CustomAdmissionDate]
        public DateTime? SchoolEnd { get; set; }
    }
    • 验证类**
public class CustomAdmissionDate : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var model = (DataModel)validationContext.ObjectInstance;

            if (model.SchoolEnd <= model.SchoolStart)
            {
                return new ValidationResult("End date must be greater than the start date.");
                
            }

            return ValidationResult.Success;
        }
    }
    • 后端发布方法**
[HttpPost]
    public IActionResult validate(DataModel model)
    {
        if (!ModelState.IsValid)
        {
             return View(model);
        }
             return View();
   }
    • 查看**
@model DataModel

 <form asp-action="Validate">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Test" class="control-label"></label>
                <input asp-for="Test" class="form-control" />
                <span asp-validation-for="Test" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="SchoolStart" class="control-label"></label>
                <input asp-for="SchoolStart" class="form-control" />
                <span asp-validation-for="SchoolStart" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="SchoolEnd" class="control-label"></label>
                <input asp-for="SchoolEnd" class="form-control" />
                <span asp-validation-for="SchoolEnd" class="text-danger"></span>
            </div>
            <button type="submit">Submit</button>
  </form>

@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
}
    • 演示:**

========更新========
如果要实现客户端验证,则需要创建客户端脚本,请将CustomAdmissionDate类更新为:

public class CustomAdmissionDate : ValidationAttribute, IClientModelValidator
    {
        public void AddValidation(ClientModelValidationContext context)
        {

            context.Attributes.Add("data-val", "true");
            context.Attributes.Add("data-val-date", "End date must be greater than the start date.");
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            //........
        }
    }

然后在wwwroot/js/Date.js中创建名为Date.js的js文件

    • 日期. js**
jQuery.validator.addMethod("date",
    function (value, element, param) {
        var starttime = document.getElementById("SchoolStart").value;
        var start = Date.parse(starttime);
        var end = Date.parse(value)
        if (end <= start) { 
            return false;
        }
        else {
            return true;
        }
    });

然后在您的页面中引用此js文件:
<script src="~/js/Date.js"></script>
现在可以在客户端站点中进行验证。

相关问题