0

I have a class like below (simplified) and I like to replace the default validation error with my own messages. I know I can set the message directly when I am adding the attribute, but it is too much pain and it is error prune to add the same message 10,000 time in the same code.

Public class CreateUser
{
    [Required(ErrorMessage = "A specific message")]//Don't like to do this
    [MinLength(10)]// Like to write specific text 
    public string Name { get; set; }
}

I have looked into their code and apparently they are using ResourceManager to set the actual message from the key. The documentation just describes the way to handle windows applications. How do I add the resources in Asp.net core application and overwrite the default values?

Update

From the code it seems like a small change if one knows how to set the ResourceManager, but from the comments it seems like a hard job. I don't understand why. For MinLenghtAttribute we have

public class MinLengthAttribute : ValidationAttribute
{
public MinLengthAttribute(int length)
        : base(SR.MinLengthAttribute_ValidationError)
    {
        Length = length;
    }
    // The rest of the code
 }

The SR is as below :

namespace System
{
    internal static partial class SR
    {
   internal static string MinLengthAttribute_ValidationError => 
    GetResourceString("MinLengthAttribute_ValidationError");
    // The rest of the code
    }
 }

One should be able to chance the value in GetResourceString and achieve my goal. Right?

Ashkan S
  • 10,464
  • 6
  • 51
  • 80
  • Looks like this article will give you what you need (https://enterprisecraftsmanship.com/posts/combining-asp-net-core-attributes-with-value-objects/) – Ryan Wilson Apr 14 '20 at 12:30
  • surely you should only be changing the error message in the case that you have a specific piece ov information to provide? such as "A name of at least 10 characters in length is required to create a user.". The others being left as the default would make it easier to find cases where you've not provided a specific error message and indicate a place for improvement. – IAmJersh Apr 14 '20 at 12:41
  • @RyanWilson Not really. He has just implemented his own ValidationAttributes. I prefer to use the built-in attributes if possible – Ashkan S Apr 14 '20 at 12:41
  • @TheHitchenator That is exactly my point. But I don't want to use the default error message from dotnet. The requirement of my project is to return a specific general error message for each of the cases. – Ashkan S Apr 14 '20 at 12:45
  • @RyanWilson Why would I need to do that? It is just a text that the validation is returning and it is reading it from the resource manager. If I know how I can change the values in ResourceManager in dotnet core I will achieve my goal. Is it such a big deal to do? – Ashkan S Apr 14 '20 at 13:46
  • @AshkanSirous This article uses implementation of `IValidationMetadataProvider` along with a custom resource file -> (https://stackoverflow.com/questions/40788092/how-to-provide-localized-validation-messages-for-validation-attributes) then adds to services in the startup. Perhaps this will be more to your liking. Answer by `jlchavez` – Ryan Wilson Apr 14 '20 at 14:01

0 Answers0