Examples pleeeeeease?
Please throw us a bone: give us some code snippets. This is as fluid as water in a raging river, to the point of being discouraging!
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: dbe7a1a4-9a3a-e81d-674a-167514aa71b0
- Version Independent ID: 72d842f6-4a81-dc27-73da-1138d4b1396e
- Content: ValidationAttribute.ErrorMessageResourceType Property (System.ComponentModel.DataAnnotations)
- Content Source: xml/System.ComponentModel.DataAnnotations/ValidationAttribute.xml
- Product: dotnet-api
- GitHub Login: @dotnet-bot
- Microsoft Alias: dotnetcontent
Hi @timmi4sa , did you figure this out? I am looking for ways on how to use the "ErrorMessageResourceType" in dotnet core to reference the type of a controller who's resx files are under: projectroot/Resources/Controllers/MyController.resx projectroot/Resources/Controllers/MyController.fr.resx
[StringLength(255)]
[Required(AllowEmptyStrings = false, ErrorMessageResourceName ="MyPropertyRequired", ErrorMessageResourceType = typeof(IStringLocalizer<MyController>????) )]
public string MyProperty { get; set; }
I have it working with the shared resource files under: projectroot/Resources/SharedResource.resx projectroot/Resources/SharedResource.fr.resx
and using the following in startup.cs:
services.AddMvc()
.AddDataAnnotationsLocalization(options => {
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(SharedResource));
});
[StringLength(255)]
[Required(AllowEmptyStrings = false, ErrorMessage ="MyPropertyRequired" )]
public string MyProperty { get; set; }
But it would be neat to be able to have those translations picked up from their respective controller rather than from the shared.
Thoughts? Any help is greatly appreciated. Thank you!
No, a complete dead-end! The annoying thing is the "ErrorMessage" property being repurposed, which not only sounds wrong but also breaks libraries (such as "Foolproof" validation library, I have stumbled upon this while porting the library from .NET 4.0 framework for my personal needs).
I am thinking of switching to Python.. (kidding)
@tonyawad88 and @timmi4sa : After close to 7 years, you probably moved on or found the solution, but if anybody has the same issue, what you should put in ErrorMessageResourceType is the type of the class containing the error message, that is typeof(MyController) in your first example.
It should be associated with ErrorMessageResourceName such as ErrorMessageResourceName = nameof(MyController.MyPropertyRequired).
Full example:
[Required(AllowEmptyStrings = false, ErrorMessageResourceName = nameof(MyController.MyPropertyRequired), ErrorMessageResourceType = typeof(MyController))]
public string MyProperty { get; set; }