实现 IValidatableObject 时的自定义错误消息
Custom Error Message while implementing IValidatableObject
我已经实施 IValidatableObject
没有任何问题:
public class DisclaimerCBs : IValidatableObject
{
public bool cb1 { get; set; } = false;
public bool cb2 { get; set; } = false;
public bool cb3 { get; set; } = false;
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
Type type = validationContext.ObjectInstance.GetType();
IEnumerable<PropertyInfo> checkBoxeProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.PropertyType == typeof(bool));
bool allIsChecked = true;
foreach (PropertyInfo checkBoxProperty in checkBoxeProperties)
{
var isChecked = (bool)checkBoxProperty.GetValue(validationContext.ObjectInstance);
if (!isChecked)
{
allIsChecked = false;
break;
}
}
if(!allIsChecked)
yield return new ValidationResult("Please agree to all Disclaimers. ");
//TRUE IF THIS FAR
yield return ValidationResult.Success;
}
}
但是,这只会在验证摘要中显示错误消息。我还想让这个错误将焦点放在指定的 <div>
元素上,并在标签中重申错误,就像 属性 验证器使用 <span asp-validation-for="Model.Property"></span>
所做的那样。我怎样才能做到这一点?
将您的 Validate
方法替换为以下内容:
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
Type type = validationContext.ObjectInstance.GetType();
IEnumerable<PropertyInfo> checkBoxeProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.PropertyType == typeof(bool));
List<ValidationResult> validationResult = new List<ValidationResult>();
foreach (PropertyInfo checkBoxProperty in checkBoxeProperties)
{
var isChecked = (bool)checkBoxProperty.GetValue(validationContext.ObjectInstance);
if (!isChecked)
{
validationResult.Add(new ValidationResult("Please agree to this Disclaimer.", new[] { checkBoxProperty.Name }));
}
}
return validationResult;
}
我已经实施 IValidatableObject
没有任何问题:
public class DisclaimerCBs : IValidatableObject
{
public bool cb1 { get; set; } = false;
public bool cb2 { get; set; } = false;
public bool cb3 { get; set; } = false;
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
Type type = validationContext.ObjectInstance.GetType();
IEnumerable<PropertyInfo> checkBoxeProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.PropertyType == typeof(bool));
bool allIsChecked = true;
foreach (PropertyInfo checkBoxProperty in checkBoxeProperties)
{
var isChecked = (bool)checkBoxProperty.GetValue(validationContext.ObjectInstance);
if (!isChecked)
{
allIsChecked = false;
break;
}
}
if(!allIsChecked)
yield return new ValidationResult("Please agree to all Disclaimers. ");
//TRUE IF THIS FAR
yield return ValidationResult.Success;
}
}
但是,这只会在验证摘要中显示错误消息。我还想让这个错误将焦点放在指定的 <div>
元素上,并在标签中重申错误,就像 属性 验证器使用 <span asp-validation-for="Model.Property"></span>
所做的那样。我怎样才能做到这一点?
将您的 Validate
方法替换为以下内容:
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
Type type = validationContext.ObjectInstance.GetType();
IEnumerable<PropertyInfo> checkBoxeProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.PropertyType == typeof(bool));
List<ValidationResult> validationResult = new List<ValidationResult>();
foreach (PropertyInfo checkBoxProperty in checkBoxeProperties)
{
var isChecked = (bool)checkBoxProperty.GetValue(validationContext.ObjectInstance);
if (!isChecked)
{
validationResult.Add(new ValidationResult("Please agree to this Disclaimer.", new[] { checkBoxProperty.Name }));
}
}
return validationResult;
}