如何格式化来自自定义验证器的错误消息
How to format error message from a custom validator
我用这种方式创建了一个自定义验证器:
public class IntArrayRequiredAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if ((!(value is int[] array) || !array.Any() || array.Any(item => item == 0)))
return new ValidationResult(this.ErrorMessage);
return ValidationResult.Success;
}
}
并将其应用于模型 属性:
[IntArrayRequiredAttribute(ErrorMessage = "You must select {0}.")]
[Display(Name = "Rol")]
public int[] Roles { get; set; }
嗯,当验证失败时,显示这个错误:
"You must select {0}."
如何 return 错误消息,以便 {0} 自动替换为字段的显示名称,例如内置验证器?
预期结果应该是"You must select Rol."
编辑:
通过查看 ValidationAttribute 源代码,我阅读了:
public ValidationResult GetValidationResult(object value, ValidationContext validationContext) {
if (validationContext == null) {
throw new ArgumentNullException("validationContext");
}
ValidationResult result = this.IsValid(value, validationContext);
// If validation fails, we want to ensure we have a ValidationResult that guarantees it has an ErrorMessage
if (result != null) {
bool hasErrorMessage = (result != null) ? !string.IsNullOrEmpty(result.ErrorMessage) : false;
if (!hasErrorMessage) {
string errorMessage = this.FormatErrorMessage(validationContext.DisplayName);
result = new ValidationResult(errorMessage, result.MemberNames);
}
}
return result;
}
我看到它调用了我重写的 IsValid 方法并格式化了消息。为什么我的情况没有格式化?
如果我使用 IsValid 重载,它的格式正确,但是,我需要使用此方法,因为我需要 validationContext 用于其他验证目的。
我认为参考源与真实代码不匹配,因为反射显示:
public ValidationResult GetValidationResult(object value, ValidationContext validationContext)
{
if (validationContext == null)
{
throw new ArgumentNullException("validationContext");
}
ValidationResult validationResult = IsValid(value, validationContext);
if (validationResult != null && (validationResult == null || string.IsNullOrEmpty(validationResult.ErrorMessage)))
{
string errorMessage = FormatErrorMessage(validationContext.DisplayName);
validationResult = new ValidationResult(errorMessage, validationResult.MemberNames);
}
return validationResult;
}
因此,如果您想将所有内容都放入单个重载中
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {...}
你可以让基础 class 进行 ErrorMessage 格式化:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if ((!(value is int[] array) || !array.Any() || array.Any(item => item == 0)))
{
return new ValidationResult(null);
}
return ValidationResult.Success;
}
或者您可以自己进行格式化:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if ((!(value is int[] array) || !array.Any() || array.Any(item => item == 0)))
{
string errorMessage = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(errorMessage);
}
return ValidationResult.Success;
}
我用这种方式创建了一个自定义验证器:
public class IntArrayRequiredAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if ((!(value is int[] array) || !array.Any() || array.Any(item => item == 0)))
return new ValidationResult(this.ErrorMessage);
return ValidationResult.Success;
}
}
并将其应用于模型 属性:
[IntArrayRequiredAttribute(ErrorMessage = "You must select {0}.")]
[Display(Name = "Rol")]
public int[] Roles { get; set; }
嗯,当验证失败时,显示这个错误:
"You must select {0}."
如何 return 错误消息,以便 {0} 自动替换为字段的显示名称,例如内置验证器?
预期结果应该是"You must select Rol."
编辑:
通过查看 ValidationAttribute 源代码,我阅读了:
public ValidationResult GetValidationResult(object value, ValidationContext validationContext) {
if (validationContext == null) {
throw new ArgumentNullException("validationContext");
}
ValidationResult result = this.IsValid(value, validationContext);
// If validation fails, we want to ensure we have a ValidationResult that guarantees it has an ErrorMessage
if (result != null) {
bool hasErrorMessage = (result != null) ? !string.IsNullOrEmpty(result.ErrorMessage) : false;
if (!hasErrorMessage) {
string errorMessage = this.FormatErrorMessage(validationContext.DisplayName);
result = new ValidationResult(errorMessage, result.MemberNames);
}
}
return result;
}
我看到它调用了我重写的 IsValid 方法并格式化了消息。为什么我的情况没有格式化?
如果我使用 IsValid 重载,它的格式正确,但是,我需要使用此方法,因为我需要 validationContext 用于其他验证目的。
我认为参考源与真实代码不匹配,因为反射显示:
public ValidationResult GetValidationResult(object value, ValidationContext validationContext)
{
if (validationContext == null)
{
throw new ArgumentNullException("validationContext");
}
ValidationResult validationResult = IsValid(value, validationContext);
if (validationResult != null && (validationResult == null || string.IsNullOrEmpty(validationResult.ErrorMessage)))
{
string errorMessage = FormatErrorMessage(validationContext.DisplayName);
validationResult = new ValidationResult(errorMessage, validationResult.MemberNames);
}
return validationResult;
}
因此,如果您想将所有内容都放入单个重载中
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {...}
你可以让基础 class 进行 ErrorMessage 格式化:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if ((!(value is int[] array) || !array.Any() || array.Any(item => item == 0)))
{
return new ValidationResult(null);
}
return ValidationResult.Success;
}
或者您可以自己进行格式化:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if ((!(value is int[] array) || !array.Any() || array.Any(item => item == 0)))
{
string errorMessage = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(errorMessage);
}
return ValidationResult.Success;
}