Ends in:
7 DAYS
13 HRS
10 MIN
11 SEC
Ends in:
7 D
13 H
10 M
11 S
Open Closed

How to get the correct message instead of "Identity.Default" when adding more validation on creating user #3962


User avatar
0
Merna created

Hello , I have a custom validation on creating User so I Used IUserValidator<IdentityUser> but I get message not localized correctly although it exists in my localization files with key :"Volo.Abp.Identity:PhoneNumberStartsWithZero" Here is the validate function

  • ABP Framework version: v5.1
  • UI type: MVC
  • DB provider: EF Core

3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Could you provide the full steps to reproduce? thanks.

  • User Avatar
    0
    Merna created

    Hello , Here is the steps: 1- Add the custom validator in domain layer and its configurations 2- try to add new user from from administration (his phone number start with 0), but instead of getting phone number... msg ,it gets me "identity.Default"

    here is the sample of a validation action called in validateAsync==>

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    ABP is designed to be compatible with ASP.NET Core Identity and will try to localize error code, but it doesn't support custom errors.

    The solution is very simple, you just need to throw a BusinessException:

    public const string PhoneNumberStartsWithZeroError = "MyProjectName:PhoneNumberStartsWithZero";
    
    public Task<IdentityResult> ValidateAsync(UserManager<IdentityUser> manager, IdentityUser user)
    {
        throw new BusinessException(PhoneNumberStartsWithZeroError);
    }
    Configure<AbpExceptionLocalizationOptions>(options =>
    {
        options.MapCodeNamespace("MyProjectName", typeof(MyProjectNameResource));
    });
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06