簡體   English   中英

ASP.NET razor 頁面上的自定義驗證屬性的核心客戶端驗證

[英]ASP.NET Core client side validation for custom validation attribute on razor page

目前,這是我的 model class 的樣子,帶有自定義驗證屬性

客戶端.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;

    }
}

我希望能夠在提交表單時進行客戶端驗證。 我的表單的所有其他字段都從客戶端工作,除了我擁有的自定義驗證,通過執行這樣的操作

 <span class="text-danger col-3" asp-validation-for="Client.Name"></span>

如何讓自定義驗證屬性以相同的方式工作? (注意:當我在我的 razor 頁面中包含一個名為“ValidationScriptsPartial”的文件時,客戶端腳本有效,其中引用了多個 jquery 文件。我相信這就是客戶端驗證的發生方式)

如何讓自定義驗證屬性以相同的方式工作?

如果您想在客戶端執行與 model 屬性上的自定義服務器端驗證屬性相同的驗證邏輯。

您可以嘗試根據您的實際需求實現自定義客戶端驗證,更多信息請參考此文檔:

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

此外,如果可能,您可以嘗試使用[Remote] 屬性,該屬性也使我們能夠驗證字段組合,這可能有助於您輕松實現相同的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM