asp.net-core - 将数据从自定义验证属性传递到页面/视图
问题描述
假设我有一个自定义验证属性:
public class CustomAttribute : ValidationAttribute
{
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var customErrorString = "You did something wrong!"; //How to pass this to the localized string defined in the resource file?
if(SomethingIsInvalid())
return new ValidationResult(FormatErrorMessage(validationContext.MemberName));
return ValidationResult.Success;
}
}
public class CustomAttributeAdapter : AttributeAdapterBase<CustomAttribute>
{
public CustomAttributeAdapter(CustomAttribute attribute, IStringLocalizer stringLocalizer)
: base(attribute, stringLocalizer)
{
}
public override void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
MergeAttribute(context.Attributes, "data-val", "true");
}
public override string GetErrorMessage(ModelValidationContextBase validationContext)
{
if (validationContext == null)
{
throw new ArgumentNullException(nameof(validationContext));
}
return GetErrorMessage(validationContext.ModelMetadata, validationContext.ModelMetadata.GetDisplayName());
}
}
比如说,我如何将数据注释中的字符串传递给验证标签?例如:
[Custom(ErrorMessage = "CustomValidationMessage")]
public string ValidateThis { get; set; }
CustomValidationMessage
在资源文件中定义并导致"This is invalid:"
现在我的问题是,如何customErrorString
从验证属性传递到本地化字符串,以便它显示在验证标签上,如下所示:
<span id="validation-span" asp-validation-for="@Model.ValidateThis" class="text-danger">This is invalid: You did something wrong!</span>
我希望我的问题很清楚。如果没有,请随时询问更多详细信息。
编辑:我让它工作:
public class CustomAttribute : ValidationAttribute
{
//Create property to hold our custom error string
public string CustomErrorString { get; set; }
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//Set the custom error string property.
CustomErrorString = "You did something wrong!";
if(SomethingIsInvalid())
return new ValidationResult(FormatErrorMessage(validationContext.MemberName));
return ValidationResult.Success;
}
}
public class CustomAttributeAdapter : AttributeAdapterBase<CustomAttribute>
{
//Declare class variable to hold the attribute's custom error string.
private string _customErrorString = string.empty;
public CustomAttributeAdapter(CustomAttribute attribute, IStringLocalizer stringLocalizer)
: base(attribute, stringLocalizer)
{
//Set the adapter's custom error string according to the attribute's custom error string
if(!string.IsNullOrEmpty(attribute.CustomErrorString))
_customErrorString = attribute.CustomErrorString;
}
public override void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
MergeAttribute(context.Attributes, "data-val", "true");
}
public override string GetErrorMessage(ModelValidationContextBase validationContext)
{
if (validationContext == null)
{
throw new ArgumentNullException(nameof(validationContext));
}
//Pass the custom error string instead of member name
return GetErrorMessage(validationContext.ModelMetadata, _customErrorString);
}
}
完成所有这些之后,您可以像这样设置资源字符串:
[Custom(ErrorMessage = "CustomValidationMessage")]
public string ValidateThis { get; set; }
CustomValidationMessage
结果在哪里"This is invalid: {0}"
通常,{0}
会导致属性的本地化成员名称。但是因为我们在适配器中传递了自定义的错误字符串,所以会被设置为自定义的错误信息。
它可能有点脏,但它可以完成工作。
解决方案
你不能把它传回去。在属性上设置的任何内容都是静态的,因为属性是在适当位置实例化的,即以后没有机会修改那里的任何内容。通常,错误消息将作为格式字符串(“This is invalid: {0}”)传递,然后验证代码将使用它以及诸如string.Format
填写成员名称之类的内容。
本地化不是属性需要担心的问题。您只需要添加数据注释本地化器:
services.AddControllers()
.AddDataAnnotationsLocalization();
推荐阅读
- objective-c - 验证输入是否为数字以及数字是否重复?
- c# - 在 C# WPF 中恢复下载
- javascript - 循环无穷大在哪里?
- c - Yocto / Poky:在不同的层上安装和使用共享库 .so
- mongodb - MongoDB - 未授权执行命令
- python - 在 Holoviews/Datashader 中更改分类数据的颜色图
- python - 使用 Python 和 Selenium 查找用户名和密码的元素。
- css - 如何在 mat-expansion-panel => css grid 中嵌套 *position:sticky*
- python - 在 Pandas 数据框中分隔共享同一列的月份和年份
- java - 如何在使用 Spring Boot 的 REST Web 服务中的 POST 请求期间修复“HTTP-415”错误