实现IValidatabletableObject时的自定义错误消息

时间:2018-12-12 15:12:33

标签: razor asp.net-core unobtrusive-validation

我已实现postDelayed(),没有任何问题:

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>一样。我该怎么办?

1 个答案:

答案 0 :(得分:1)

用以下内容替换您的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; } 方法:

$composer.phar
$export PATH=/usr/local/php5/bin:$PATH
$composer.phar --version