【发布时间】: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;
}
}
我希望能够在提交表单时进行客户端验证。我的表单的所有其他字段都从客户端工作,除了我拥有的自定义验证,通过这样做
<span class="text-danger col-3" asp-validation-for="Client.Name"></span>
如何让自定义验证属性以相同的方式工作? (注意:当我在我的 razor 页面中包含一个名为“ValidationScriptsPartial”的文件时,客户端脚本可以工作,其中引用了多个 jquery 文件。我相信客户端验证就是这样发生的)
【问题讨论】:
标签: c# jquery asp.net asp.net-core razor