Search code examples
c#asp.net-mvcfluentvalidation

Remove property name from validation message


Is it possible to remove property name form the validation message? For example, instead of:

Field 'Name' should not be empty.

I want to show:

Field should not be empty.

I need to do this global, for all validators.


Solution

  • You can do this using the localization customization like so to make the change globally. You can then of course override specific errors with a custom format if you need a one-off change.

    ValidatorOptions.ResourceProviderType = typeof(MyResources);
    

    ...

    public class MyResources {
       public static string notempty_error {
          get { 
              return "Field should not be empty.";
          }
       }
    }