Open Closed

Need to include localization in DTO validation #3482


User avatar
0
KirstyLH created
  • ABP Framework version: v5.2.2
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

We are using a data grid component which requires validation to be performed within the dto class. The validation works, but we are unable to use a localized error message within the class. Is there a way to refer to the localization resource from within the dto?

For eg:

public class ApplicationValidationActionFlatDto
{

    public Guid Id { get; set; }
    public string TaskDescription { get; set; }
    public string Result { get; set; }

    [CustomValidationResultDate]
    public DateTime ResultDate { get; set; }
    public Guid? ResultedById { get; set; }
    public string Comments { get; set; }
    public int Order { get; set; }
    public Guid ApplicationProcessId { get; set; }
    public Guid LicenceTypeValidationId { get; set; }

}

public class CustomValidationResultDate : ValidationAttribute
{

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        if (value != null)
        {

            var d = Convert.ToDateTime(value);
            if (d.Year.ToString().Length < 4)
            {
                return new ValidationResult("ValidationMessageResultDate");
            }
            else if (d < DateTime.Now)
            {
                return new ValidationResult("ValidationMessageResultDateLessThanCurrentDate");
            }

        }

        return ValidationResult.Success;

    }

}

In CustomValidationResultDate Class, the parameter of ValidationResult Class is not able to be set to a localized string.


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

    hi

    https://docs.abp.io/en/abp/latest/Validation#ivalidatableobject https://docs.abp.io/en/abp/latest/Validation#resolving-a-service

  • User Avatar
    0
    KirstyLH created

    Hi,

    I'm sorry, but I don't understand how those links show localisation in dtos. I don't want to change our validation component - we are using a third party component which uses this validation type. The validation itself is working. What I'm unsure of is if I can inject our Localisation resource into a dto to reference that for the error message which currently is a hard-coded string. The links above also use a hard-coded error message string. Is there anything I can do?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You just need to inject the services in the IsValid method.

    var myService = validationContext.GetRequiredService<IMyService>();

    You can also manually validate in the application service method.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 20, 2025, 18:00