Is there a way to use DataAnnotations for validation, particulary in MVC, while relying on a custom resource provider ?
I built a custom SQL based resource provider for the asp.net side of my MVC application. At this point I'm down in the model section and want to use DataAnnotations for validation. Something like ...
I recognize that the above isn't quite correct syntax, i.e. ErrorMessageResourceType, but I just wanted to convey the type of thing I'm trying to do. It seems that I could inherit from each validation type and override the .Message property. Looking
at the Validation Atrributes disassembly it there is an interesting protected constructor "protected ValidationAttribute(Func<string> errorMessageAccessor)". This seems like a valid option when I inherit from the existing attributes, but I can't find any
real documentation that constructor.
It just seems strange that I spent all this time building a resource provider for asp.net and there isn't a way to resuse it (even if some re-jiggering is required). It's seems quite odd that annotations can only be internationlized with embedded resource
files. Am I off my rocker?
Thanks for the confirmation. Deriving the attributes and created "RequiredLocalized" "StringLengthLocalized" etc... is where I ended up.
While I don't know if this is of interest to you, it may be of interest to someone else in my situation.
I ended up putting the lookup in the IsValid() method rather than the FormatErrorMessage()
I'm misusing the ErrorMessageResourceType field (see how i do it in the overload). The point of my misuse is that i take the full name of the type ...in this case something like MyCompany.Project.UI.SomeClass and that data becomesa a Resource in my
lookup tables, which has ResourceItems. This is very similar to the way page url's are listed in the default asp.net membership provider tables.
A basic attribute (using embedded resource files) looks like this ...
There are a number of reasons I want my DB to store internationalized errors.
Client requirement for errors to be internationlized
Web farm environment, so distribution of resources becomes a code-roll out when a change is needed
Clients require a web based editior for customization of the messages.
Already have a centralized resource store (DB) and don't want fragmentation.
Resource files tend to tie the internationalization process to development, rather than contnet/translation people.
Commonality with my custom asp.net sql resource provider
The site has a centralized error reporting / monitoring as a feature. The moment I have errors stored outside by DB my error log is linking back to non-database resource, i.e., I've broken declarative referential integrity (30% of the reason i hired the
database to begin with).
Hey I saw your post. I am looking to do something very simliar. I was wondering if you has some more sample code. First I think in your post you actaully pasted 2 code bits that were the same thing. So questions...
1. Does client side validation work?
2. Can you give me a example of an attribute you wrote?
3. Is there a way to call the localization thingy from the web layer and not the data.. That is I am like the attribute, like overriding it, but I want the actual function that overrides teh text in the web layer..
I figured i would respond publicly so it's on file for the community.
> 2 code bits that were the same thing
I went back and edited the earlier post fixing a typo. Part of my point, however, was that the only difference between calling a built in attribute (pulling data from a resource file) and my own Localized attribute is the name of said attribute. So both
_Required_ and my version _RequiredLocalized_ use the ErrorMessageResourceType and ErrorMessageResourceName parameters.
> Does client side validation work?
I'm not sure yet. I haven't turned on client side validation as of yet. Although, client side validation just pumps out a set of instructions that "MicrosoftMvcJQueryValidation.js" interprets and gets to work. In effect its a block of JS dropped at the
bottom of your page. At worst I fake it and dump out my own instructions.
> an example of an attribute you wrote?
There really isn't much too it. Each attribute just overrides the IsValid function and calls a helper function to localize the message based on : ErrorMessageResourceType, ErrorMessageResourceName and the current threads UI Culture. The code is as follows...
> Is there a way to call the localization thingy from the web layer and not the data
I'm actually pretty agnostic about this. Attribute validation is written so it can apply in all sorts of contexts, not just web. If you're writing MVC the attributes are instantiated and called by the default model binder before the controller action
is called. The point is the validation happens at the point where the attributes are instantiated.
Now, if you want some sort of UI layer that does conversions you might want to avoid my strategy all together. Instead of throwing error messages you throw a unique error code/tokens. Say for example on a site register page the users email is in a bad
format. Instead of a user friendly error message pass something like: "Register.Validation.EmailInBadFormat." Then just before display you chunk over your error collection and translate from the token to the message.
> [Me asking myslef] Is this the right strategy?
Attribute validaiton is great for simple validations (i.e. username over 5 chars?). It is also interesting because testing becomes declarative rather than relying on writing structured code. The model falls down, IMHO, when validation is more complicated.
Examples: password valid against a history table or selection A requires field B's choices to be restricted, . Attributes don't really handle this well without becoming business logic objects. The site i'm working on is a large MVC site. I ended up using
a mix of attributes for basic validation and a validation layer for the more complicated tests. At some points the attributes feel great, at other points having my validation split up feels bad. So, for me, the jury is still out.
Eric Barr
Member
4 Points
7 Posts
Internationalize DataAnnotations error messages using a custom resource provider (SQL - not embed...
Mar 18, 2010 01:57 PM|LINK
Hopefully this is the right forum (http://social.msdn.microsoft.com/forums/en-US/clr/threads/) sent me over here..
Is there a way to use DataAnnotations for validation, particulary in MVC, while relying on a custom resource provider ?
I built a custom SQL based resource provider for the asp.net side of my MVC application. At this point I'm down in the model section and want to use DataAnnotations for validation. Something like ...
<div style="BACKGROUND-COLOR: white; COLOR: black" mce_style="BACKGROUND-COLOR: white; COLOR: black"> </div>I recognize that the above isn't quite correct syntax, i.e. ErrorMessageResourceType, but I just wanted to convey the type of thing I'm trying to do. It seems that I could inherit from each validation type and override the .Message property. Looking at the Validation Atrributes disassembly it there is an interesting protected constructor "protected ValidationAttribute(Func<string> errorMessageAccessor)". This seems like a valid option when I inherit from the existing attributes, but I can't find any real documentation that constructor.
It just seems strange that I spent all this time building a resource provider for asp.net and there isn't a way to resuse it (even if some re-jiggering is required). It's seems quite odd that annotations can only be internationlized with embedded resource files. Am I off my rocker?
</div>localization DataAnnotations mvc - 2
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Internationalize DataAnnotations error messages using a custom resource provider (SQL - not e...
Mar 18, 2010 04:30 PM|LINK
There is no built-in support in DataAnnotations for this.
You will need to derive from the attribute(s) in question and override the FormatErrorMessage() method to do the database lookup.
Eric Barr
Member
4 Points
7 Posts
Re: Internationalize DataAnnotations error messages using a custom resource provider (SQL - not e...
Mar 19, 2010 02:58 PM|LINK
Brad,
Thanks for the confirmation. Deriving the attributes and created "RequiredLocalized" "StringLengthLocalized" etc... is where I ended up.
While I don't know if this is of interest to you, it may be of interest to someone else in my situation.
A basic attribute (using embedded resource files) looks like this ...
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Internationalize DataAnnotations error messages using a custom resource provider (SQL - not e...
Apr 03, 2010 01:50 AM|LINK
Resouce files have been used for many years to contain localized error messages. I'm just curious why you need dynamic error messages?
Eric Barr
Member
4 Points
7 Posts
Re: Internationalize DataAnnotations error messages using a custom resource provider (SQL - not e...
Apr 03, 2010 12:01 PM|LINK
There are a number of reasons I want my DB to store internationalized errors.
HTH,
-eric
Eric Barr
Member
4 Points
7 Posts
Re: Internationalize DataAnnotations error messages using a custom resource provider (SQL - not e...
May 28, 2010 07:40 AM|LINK
I was privately asked ...
Hey I saw your post. I am looking to do something very simliar. I was wondering if you has some more sample code. First I think in your post you actaully pasted 2 code bits that were the same thing. So questions...
1. Does client side validation work?
2. Can you give me a example of an attribute you wrote?
3. Is there a way to call the localization thingy from the web layer and not the data.. That is I am like the attribute, like overriding it, but I want the actual function that overrides teh text in the web layer..
I figured i would respond publicly so it's on file for the community.
> 2 code bits that were the same thing
I went back and edited the earlier post fixing a typo. Part of my point, however, was that the only difference between calling a built in attribute (pulling data from a resource file) and my own Localized attribute is the name of said attribute. So both _Required_ and my version _RequiredLocalized_ use the ErrorMessageResourceType and ErrorMessageResourceName parameters.
> Does client side validation work?
I'm not sure yet. I haven't turned on client side validation as of yet. Although, client side validation just pumps out a set of instructions that "MicrosoftMvcJQueryValidation.js" interprets and gets to work. In effect its a block of JS dropped at the bottom of your page. At worst I fake it and dump out my own instructions.
> an example of an attribute you wrote?
There really isn't much too it. Each attribute just overrides the IsValid function and calls a helper function to localize the message based on : ErrorMessageResourceType, ErrorMessageResourceName and the current threads UI Culture. The code is as follows...
> Is there a way to call the localization thingy from the web layer and not the data
I'm actually pretty agnostic about this. Attribute validation is written so it can apply in all sorts of contexts, not just web. If you're writing MVC the attributes are instantiated and called by the default model binder before the controller action is called. The point is the validation happens at the point where the attributes are instantiated.
Now, if you want some sort of UI layer that does conversions you might want to avoid my strategy all together. Instead of throwing error messages you throw a unique error code/tokens. Say for example on a site register page the users email is in a bad format. Instead of a user friendly error message pass something like: "Register.Validation.EmailInBadFormat." Then just before display you chunk over your error collection and translate from the token to the message.
> [Me asking myslef] Is this the right strategy?
Attribute validaiton is great for simple validations (i.e. username over 5 chars?). It is also interesting because testing becomes declarative rather than relying on writing structured code. The model falls down, IMHO, when validation is more complicated. Examples: password valid against a history table or selection A requires field B's choices to be restricted, . Attributes don't really handle this well without becoming business logic objects. The site i'm working on is a large MVC site. I ended up using a mix of attributes for basic validation and a validation layer for the more complicated tests. At some points the attributes feel great, at other points having my validation split up feels bad. So, for me, the jury is still out.
Hope that helps.