【问题标题】:ASP.NET Core client side validation for custom validation attribute on razor page剃须刀页面上自定义验证属性的 ASP.NET Core 客户端验证
【发布时间】:2020-09-21 14:14:52
【问题描述】:

目前,这是我的模型类的样子,带有自定义验证属性

Client.cs

    [Required]
    [DisplayName("Bookkeeping")]
    public bool Bookkeeping { get; set; }

    [Required]
    [DisplayName("Personal Income Taxation")]
    public bool Personal_Income_Taxation { get; set; }

    [Required]
    [DisplayName("Self-Employed Business Taxes")]
    public bool Self_Employed_Business_Taxes { get; set; }

    [Required]
    [DisplayName("GST/PST/WCB Returns")]
    public bool GST_PST_WCB_Returns { get; set; }

    [Required]
    [DisplayName("Tax Returns")]
    public bool Tax_Returns { get; set; }

    [Required]
    [DisplayName("Payroll Services")]
    public bool Payroll_Services { get; set; }

    [Required]
    [DisplayName("Previous Year Filings")]
    public bool Previous_Year_Filings { get; set; }

    [Required]
    [DisplayName("Govt. Requisite Form Applicaitons")]
    public bool Government_Requisite_Form_Applications { get; set; }

    [StringLength(220, ErrorMessage = "Cannot exceed more than 220 characters")]
    public string Other { get; set; }

    [CheckboxAndOtherValidation(nameof(Bookkeeping),
nameof(Personal_Income_Taxation),
nameof(Self_Employed_Business_Taxes),
nameof(GST_PST_WCB_Returns),
nameof(Tax_Returns),
nameof(Payroll_Services),
nameof(Previous_Year_Filings),
nameof(Government_Requisite_Form_Applications), ErrorMessage = "At least one of the checkboxes or the 'Other' field must be filled")]
    public bool AreCheckboxesAndOtherValid { get; set; }

CheckboxAndOtherValidation.cs

public class CheckboxAndOtherValidation : ValidationAttribute
{
    readonly object TRUE = true;
    string[] _alltheOtherProperty;

    public CheckboxAndOtherValidation(params string[] alltheOthersProperty)
    {
        _alltheOtherProperty = alltheOthersProperty;
    }

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

        var errorMessage = FormatErrorMessage((validationContext.DisplayName));

        bool IsOtherNull = false;
        bool IsAnyCheckboxChecked = false;

        if (_alltheOtherProperty?.Count() > 0 != true)
        {
            return new ValidationResult(errorMessage);
        }

        var otherPropertyInfo = validationContext.ObjectType.GetProperty(nameof(Client.Other));
        if (otherPropertyInfo != null)
        {
            object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
            if (otherPropertyValue == null || string.IsNullOrEmpty(otherPropertyValue.ToString()))
            {
                IsOtherNull = true;
            }
        }

        for (var i = 0; i < _alltheOtherProperty.Length; ++i)
        {
            var prop = _alltheOtherProperty[i];
            var propertyInfo = validationContext.ObjectType.GetProperty(prop);
            if (propertyInfo == null)
            {
                continue;
            }

            object propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
            if (Equals(TRUE, propertyValue))
            {
                IsAnyCheckboxChecked = true;
            }
        }

        if (IsOtherNull && !IsAnyCheckboxChecked)
            return new ValidationResult(errorMessage);
        else
            return ValidationResult.Success;

    }
}

我希望能够在提交表单时进行客户端验证。我的表单的所有其他字段都从客户端工作,除了我拥有的自定义验证,通过这样做

&lt;span class="text-danger col-3" asp-validation-for="Client.Name"&gt;&lt;/span&gt;

如何让自定义验证属性以相同的方式工作? (注意:当我在我的 razor 页面中包含一个名为“ValidationScriptsPartial”的文件时,客户端脚本可以工作,其中引用了多个 jquery 文件。我相信客户端验证就是这样发生的)

【问题讨论】:

    标签: c# jquery asp.net asp.net-core razor


    【解决方案1】:

    如何让自定义验证属性以相同的方式工作?

    如果您想在客户端执行与模型属性的自定义服务器端验证属性相同的验证逻辑。

    您可以尝试根据您的实际需求实现自定义客户端验证,更多信息请参考此文档:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-3.1#custom-client-side-validation

    此外,如果可能,您可以尝试使用[Remote] attribute,这也使我们能够验证字段组合,这可能会帮助您轻松实现相同的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-21
      • 2013-11-12
      • 2015-03-26
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多