ASP.NET Puror 页面上自定义验证属性的核心客户端验证



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

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

复选框和其他验证.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>

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

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

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

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

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

此外,如果可能,您可以尝试使用 [Remote] 属性,该属性还使我们能够验证字段组合,这可能有助于您轻松实现相同的要求。

最新更新