- 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)
-
0
hi
https://docs.abp.io/en/abp/latest/Validation#ivalidatableobject https://docs.abp.io/en/abp/latest/Validation#resolving-a-service
-
0
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?
-
0
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.