Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default error messages of MVC Core ValidationSummary?

Using MVC Core with ASP.NET Identity I would like to change the defaults error messages of ValidationSummary that arrived from Register action. Any advice will be much appreciated.

ASP.NET Core Register Action

like image 722
AG70 Avatar asked Aug 16 '16 05:08

AG70


People also ask

How do I create a custom error message in ValidationSummary?

AddModelError() method. The ValidationSummary() method will automatically display all the error messages added in the ModelState . Thus, you can use the ValidationSummary helper method to display error messages.

How do you show custom validator error message in ValidationSummary?

In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" .

What is ValidationSummary in MVC?

ASP.NET MVC: ValidationSummary The ValidationSummary() extension method displays a summary of all validation errors on a web page as an unordered list element. It can also be used to display custom error messages.


1 Answers

You should override methods of IdentityErrorDescriber to change identity error messages.

public class YourIdentityErrorDescriber : IdentityErrorDescriber
{
    public override IdentityError PasswordRequiresUpper() 
    { 
       return new IdentityError 
       { 
           Code = nameof(PasswordRequiresUpper),
           Description = "<your error message>"
       }; 
    }
    //... other methods
}

In Startup.cs set IdentityErrorDescriber

public void ConfigureServices(IServiceCollection services)
{
    // ...   
    services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddErrorDescriber<YourIdentityErrorDescriber>();
}

The answer is from https://stackoverflow.com/a/38199890/5426333

like image 97
adem caglin Avatar answered Sep 19 '22 01:09

adem caglin