Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Customize property names for DTO Validation messages #6182


User avatar
0
inzone created

When I get UserFriendlyExceptions related to DTO Validation (AbpValidationException) the exception message detailing the properties with errors display the property name as defined in the DTO class.

Is there any way to set a localized display name for those properties so the error messages the users can get are more clear about what fields have errors?


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    see:https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1360

  • User Avatar
    0
    inzone created

    I think that's not exactly what I'm talking about.

    Look at this example.

    public class RegisterInput
    {
        [Required]
        public string FirstName { get; set; }
        
        [Required]
        public string LastName { get; set; }
    }
    

    Using that DTO with validation and provide this JSON to the app service:

    { firstName: "Emiliano", lastName: null }
    

    Then I will end up with an exception with a message like The field LastName is required

    I want to change that LastName to Last Name

    Is there any data attribute to set to the property to tell ABP to show Last Name as the display name for that property?

  • User Avatar
    0
    maliming created
    Support Team

    try DisplayName

    [DisplayName("Last Name")]
    [Required]
    public string LastName { get; set; }
    
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, most of the model validation attributes came from asp.net core. Abp only collect all the validations from those attributes and put the errors in AbpValidationException

    you can do something similar to this:

    [Required(ErrorMessage = "Last Name is required")]
    public string LastName {get;set;}
    

    Otherwise you can try using Abp Custom Validation instead

  • User Avatar
    0
    inzone created

    The DisplayName Attribute could work, but how do I hook it up with the ABP Localization source? Does AbpDisplayNameAttribute work for this?

  • User Avatar
    0
    ryancyq created
    Support Team

    Nope. AbpDisplayName is only used in Razor page.

    Use case:

    @Model.LabelFor(x => x.LastName)