【问题标题】:Passing data from custom validation attribute to page/view将数据从自定义验证属性传递到页面/视图
【发布时间】:2020-07-27 08:47:46
【问题描述】:

假设我有一个自定义验证属性:

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} 会生成属性的本地化成员名称。但是因为我们在适配器中传递了自定义的错误字符串,所以会设置为自定义的错误信息。

它可能有点脏,但它可以完成工作。

【问题讨论】:

    标签: asp.net-core validationattribute


    【解决方案1】:

    你不能把它传回去。在属性上设置的任何内容都是静态的,因为属性是就地实例化的,即以后没有机会修改任何内容。通常,错误消息将作为格式字符串(“This is invalid: {0}”)传递,然后验证代码将使用该格式字符串以及string.Format 之类的内容来填写成员名称。

    本地化不是属性需要担心的问题。您只需添加数据注释本地化器:

    services.AddControllers()
        .AddDataAnnotationsLocalization();
    

    【讨论】:

    • 我真的让它工作了。请参阅我的编辑以获取解决方案。
    猜你喜欢
    • 2011-04-07
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多