問題描述
我創建了一個自定義ResourceProvider
來從數據庫中提取本地化信息.我現在想使用 DataAnnotation
為模型添加驗證.
I have created a custom ResourceProvider
to pull localization information from a database. I now want to use DataAnnotation
to add validation to the model.
DataAnnotation
具有 ErrorMessageResourceType
和 ErrorMessageResourceName
屬性,但 ErrorMessageResourceType
只接受 System.Type
(即編譯好的資源文件)
DataAnnotation
has ErrorMessageResourceType
and ErrorMessageResourceName
properties but ErrorMessageResourceType
only accepts System.Type
(i.e. a compiled resource file)
有沒有辦法讓 DataAnnotation 使用自定義的 ResourceProvider?
Is there any way to get DataAnnotation to use the custom ResourceProvider?
推薦答案
我意識到這是一個老問題,但想補充一點.我發現自己處于同樣的情況,似乎沒有關于這個主題的任何文檔/博客.盡管如此,我還是想出了一種使用自定義資源提供程序的方法,但需要注意的是.需要注意的是,我在一個 MVC 應用程序中,所以我仍然有 HttpContext.GetLocalResourceObject()
可用.這是 asp.net 用來本地化項目的方法.資源對象的缺失不會阻止您編寫我們自己的解決方案,即使它是直接查詢數據庫表.不過,我認為值得指出.
I realize this is an old question, but wanted to add a bit. I found myself in the same situation and there doesn't appear to be any documentation/blogumentation on this topic. Nevertheless, I figured out a way to use a custom resource provider, with one caveat. The caveat is that I'm in an MVC application so I still have HttpContext.GetLocalResourceObject()
available. This is the method that asp.net uses to localize items. The absence of the resource object doesn't stop you from writing our own solution, even if its a direct query of the DB tables. Nevertheless, I thought it was worth pointing out.
雖然我對以下解決方案不太滿意,但它似乎有效.對于我想使用的每個驗證屬性,我都從所述屬性繼承并重載 IsValid().裝修是這樣的:
While I'm not terribly happy with the following solution, it seems to work. For each validation attribute I want to use I inherit from said attribute and overload the IsValid(). The decoration looks like this:
[RequiredLocalized(ErrorMessageResourceType= typeof(ClassBeginValidated), ErrorMessageResourceName="Errors.GenderRequired")]
public string FirstName { get; set; }
新屬性如下所示:
public sealed class RequiredLocalized : RequiredAttribute {
public override bool IsValid(object value) {
if ( ! (ErrorMessageResourceType == null || String.IsNullOrWhiteSpace(ErrorMessageResourceName) ) ) {
this.ErrorMessage = MVC_HtmlHelpers.Localize(this.ErrorMessageResourceType, this.ErrorMessageResourceName);
this.ErrorMessageResourceType = null;
this.ErrorMessageResourceName = null;
}
return base.IsValid(value);
}
}
注意事項
- 您需要使用派生屬性而不是標準屬性來裝飾您的代碼
- 我正在使用 ErrorMessageResourceType 來傳遞正在驗證的類的類型.我的意思是,如果我在客戶類中并驗證 FirstName 屬性,我將通過 typeof(customer).我這樣做是因為在我的數據庫后端中,我使用完整的類名(命名空間 + 類名)作為鍵(與在 asp.net 中使用頁面 URL 的方式相同).
- MVC_HtmlHelpers.Localize 只是我的自定義資源提供程序的一個簡單包裝器
- You need to decorate your code with the derived attribute, not the standard one
- I'm using ErrorMessageResourceType to pass the type of the class being validated. By that I mean if I'm in a customer class and validating the FirstName property I would pass typeof(customer). I'm doing this because in my database backend I'm using the full class name (namespace + classname) as a key (the same way a page URL is used in asp.net).
- MVC_HtmlHelpers.Localize is just a simple wrapper for my custom resource provider
(半被盜的)幫助代碼看起來像這樣......
The (semi-stolen) helper code looks like this ....
public static string Localize (System.Type theType, string resourceKey) { return Localize (theType, resourceKey, null); } public static string Localize (System.Type theType, string resourceKey, params object[] args) { string resource = (HttpContext.GetLocalResourceObject(theType.FullName, resourceKey) ?? string.Empty).ToString(); return mergeTokens(resource, args); } private static string mergeTokens(string resource, object[] args) { if (resource != null && args != null && args.Length > 0) { return string.Format(resource, args); } else { return resource; } }
這篇關于帶有自定義 ResourceProvider 的 DataAnnotation的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!