3

I'm developing a web application using ASP.NET Core 2.1. I want to override/localize default data-annotations' error messages without using any package and external dependency or even culture. For example this is Mymodel:

class Mymodel
{
    [Required]
    public string Email { get; set; }
}

When I submit the empty form it returns this default error:

The {0} field is required. (in this case {0}= Email)

is there anyway to override/customize/change this default error for all [Required] data-annotations??

I don't wanna add ErrorMessage to my required data-annotation. Like this:

class ViewModel
{
    [Required(ErrorMessage = "my customized error message")
    public string Email { get; set; }
}

Also I don't want to write my own data attribute.

I want something general. So wherever I use [Required] data-annotation, my customized error message will be shown.

I found this related question but it didn't helped me because no one answered the question.

How to provide localized validation messages for validation attributes

I checked Microsoft docs. It wasn't clear

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2#dataannotations-localization

Arian Shahalami
  • 1,369
  • 2
  • 13
  • 25
  • 1
    There is another problem here. I'm in a team. I made a subclass and other developers used the main class so that leads to disaster :(( – Arian Shahalami Jan 17 '19 at 10:31
  • 1
    It would need adapting for ASP.NET Core, but [this answer](https://stackoverflow.com/a/23179685) looks like it would work for you. – Kirk Larkin Jan 17 '19 at 10:34

1 Answers1

0

I was going to comment, but I do not have enough reputation to do it. Apparently, what you are asking is not yet possible. There is a issue in Github about this.

Sergio
  • 136
  • 2
  • 7