[英]Custom Validation Message, language specific
I have a model that looks something like this: 我有一个看起来像这样的模型:
[LocalizedRegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", "RedesignEmailValidationError")]
public string EmailAddress { get; set; }
[Compare("EmailAddress", ErrorMessage = "Emails mismatch!")]
public string EmailConfirm { get; set; }
The problem is that the Error Message is not localized. 问题是错误消息未本地化。 What would be the best approach to handle this?
处理此问题的最佳方法是什么?
PS: I receive the language specific text that needs to be on this form in a model; PS:我收到的语言特定文本需要在模型中的此表格上显示; ideally I would like to use the text provided there.
理想情况下,我想使用那里提供的文字。
You need to use ErrorMessageResourceName and ErrorMessageResourceType 您需要使用ErrorMessageResourceName和ErrorMessageResourceType
Should be something like that: 应该是这样的:
[Compare("EmailAddress", ErrorMessageResourceName = "ConfirmEmailErrorMessage", ErrorMessageResourceType=typeof(your_resource_type)]
public string EmailConfirm { get; set; }
Also if you're not relying on default resource provider, you have to implement it yourself. 另外,如果您不依赖默认资源提供程序,则必须自己实现。
Like: 喜欢:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
public sealed class LocalizedDisplayNameAttribute : DisplayNameAttribute {
public LocalizedDisplayNameAttribute() {
}
public LocalizedDisplayNameAttribute(object context) {
Context = context;
}
public object Context { get; set; }
public override string DisplayName {
get {
// TODO: override based on CultureInfo.CurrentCulture and Context here
return "LocalizedAttributeName";
}
}
}
[AttributeUsage(AttributeTargets.Property)]
public class LocalizedCompareAttribute : CompareAttribute {
public object Context { get; set; }
public LocalizedCompareAttribute(string otherProperty)
: base(otherProperty) {
}
public override string FormatErrorMessage(string name) {
// TODO: override based on CultureInfo.CurrentCulture and Context here
string format = "Field '{0}' should have the same value as '{1}'.";
return string.Format(CultureInfo.CurrentCulture, format, name, OtherPropertyDisplayName);
}
}
etc. 等等
Usage: 用法:
[LocalizedCompare("EmailAddress", Context = "ResourceKey_EmailMismatch")]
[LocalizedDisplayName("ResourceKey_Email")]
public string EmailConfirm { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.