Starts in:
2 DAYS
15 HRS
17 MIN
20 SEC
Starts in:
2 D
15 H
17 M
20 S
Open Closed

Localization in DTO / Fluent Validators #180


User avatar
0
lalitChougule created

Hi

Suppose this is my DTO

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

Or suppose this is my Validator Class

public class AnyDtoValidator : AbstractValidator<AnyDto>
    {
        public AnyDtoValidator()
        {
            RuleFor(x => x.Name).NotNull().WithMessage("Name is required");
        }
    }

I want to localize my errror message which is Name is required. How do I do it ?

Kindly provide solution in both cases DTO/Fluent Validators


6 Answer(s)
  • User Avatar
    0
    lalitChougule created

    How do we Check Duplicate Entities in Fluent Validators ? Where this duplicate check should be done? Contract/Domain/Application project ? Cause I have my Validator's in Application.Contracts project. Is this the right way to do it?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi, You can add Fluent Validators in the Application project and you can inject IStringLocalizer interface to return localized content .

  • User Avatar
    0
    lalitChougule created

    Thanks @liangshiwei

    My DTO's are as below :

    CustomerDto

    public class CustomerDto : AuditedEntityDto<Guid>
    {
        ....
    }
    

    AddressDto

    public class AddressDto : AuditedEntityDto<Guid>
    {
        ....
    }
    

    OrderDto

    public class OrderDto : AuditedEntityDto<Guid>
    {
        public CustomerDto Customer { get; set; }
        
        public AddressDto Address { get; set; }
    }
    

    As per your above solution I tried injecting IStringLocalizer<MyProjectResource> localizer as below

    CustomerDtoValidator

    public class CustomerDtoValidator : AbstractValidator<CustomerDto>
        {
            public CustomerDtoValidator(IStringLocalizer<MyProjectResource> _localizer)
            {
                ....
            }
        }
    

    AddressDtoValidator

    public class AddressDtoValidator : AbstractValidator<AddressDto>
        {
            public AddressDtoValidator(IStringLocalizer<MyProjectResource> _localizer)
            {
                ....
            }
        }
    

    But I am not able to Implement below code for OrderDto :

    public class OrderDtoValidator:AbstractValidator<SupplierCombinedDto>
        {
            public OrderDtoValidator()
            {
                RuleFor(x => x.Customer).SetValidator(new CustomerDtoValidator()); <-- ERROR
                RuleFor(x => x.Address).SetValidator(new AddressDtoValidator()); <-- ERROR
                
            }
        }
    

    ERROR: There is no argument given that corresponds to the required formal parameter '_localizer' of CustomerDtoValidator(IStringLocalizer

    What should I pass as argument ?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Try :

    public class OrderDtoValidator:AbstractValidator<SupplierCombinedDto>
        {
            public OrderDtoValidator(IStringLocalizer<MyProjectResource> _localizer)
            {
                RuleFor(x => x.Customer).SetValidator(new CustomerDtoValidator(_localizer));
                RuleFor(x => x.Address).SetValidator(new AddressDtoValidator(_localizer));
                
            }
        }
    
  • User Avatar
    0
    lalitChougule created

    Thanks @liangshiwei

    It worked, Just last question which remained unanswered before How do we Check Duplicate Entities in Fluent Validators ? I want to cross check from my Address table whether this record is already present in my table ( i.e duplicate address ) Is it possible in Fluent Validator ? And how do I do it ?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I do n’t know much about FluentValidation, you can maybe inject repository to validator class and check duplicate entities. But I suggest you Check Duplicate Entities in the domain layer.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06