Base solution for your next web application
Open Closed

Better Cross validation between properties and display validation messages to clients. #8452


User avatar
0
adamphones created

version: asp zero 8.1 angular

`public class Schedule{

public DateTime EffectiveFrom {get;set;}

public DateTime EffectiveTo {get;set;} } ` We would like to enforce user to enter EndDate greater than StartDate. We tried ICustomValidate interface as below.

public void AddValidationErrors(CustomValidationContext context) { if (EffectiveTo.HasValue && EffectiveTo.Value.Date < EffectiveFrom.Date) { context.Results.Add(new ValidationResult("Effective To must be greater than Effective From date.")); } }

The validation seems work but on the client side we get an exception as 'Method arguments are not valid! See ValidationErrors for details.' But the client cannot really see which field has the issue or whether the EffectiveTo date should be greater than Effective From date.

One way to fix this issue would be to implement validation on angular in the client side. However we were wonder whether there is a better way to represent these validation issues to the client?

Regarding to this we could only see this document https://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#introduction-to-validation

Do you have any other documentation which explains data validation?

My second question which relates to this is that whether it is possible to generically create validations? What I mean by this is that instead of having validation on each class to implement "AddValidationErrors" seperate that outside of the class. The reason being is that we have a lot of entities that has the properties EffectiveFrom and EffectiveTo and they all need validation as EffectiveTo> EffectiveFrom. It would be nice to have single validation for those properties and automatically plug it into each class through interface maybe? Any idea on this?

I would appriciate your help on this topic.

Regards,


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

    Are you using angular + asp net core or angular js?

    What I mean by this is that instead of having validation on each class to implement "AddValidationErrors" seperate that outside of the class.

    You can take a look at Fluent Validation.

    https://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#fluent-validation

  • User Avatar
    0
    adamphones created

    Thanks for coming back.

    We use angular + asp net core.

    Somehow the message we get contains square brackets. I think this indicates that the message is also localized.

    However, we add those keys in the localization xml files we still don't get the message you get in the screenshot. Any idea on that?

    And thanks for the Fluent Validation. I think this would be the way to go. However, the nuget package does not seem to install. I get the following message.

    And I assume this would work which project this should be installed? .Core or better in .Application?

    Regards,

  • User Avatar
    0
    maliming created
    Support Team

    hi adamphones

    What is the language(culture) of your application?


    You can try to install the package using .net cli or package reference

    dotnet add package Abp.FluentValidation --version 5.1.0
    
    <PackageReference Include="Abp.FluentValidation" Version="5.1.0" />
    
  • User Avatar
    0
    adamphones created

    it-s en-GB.

    Thanks I managed to install the nuget package.

  • User Avatar
    0
    adamphones created

    Ok I did a bit more investigation on this and I think i found what causing the issue. I guess it is a bug.

    What I did is to disable all other languages( including englis-en) but en-GB. As we do not want to use localization. This is causing the message. As soon as I enable english (en) then I get the same error message with you.

    Maybe this should be another question but is there any other place we should disable the localization and just make the default en-GB?

  • User Avatar
    0
    maliming created
    Support Team

    AbpWeb module does not provide en-GB localization information. You can expand it manually.

    https://aspnetboilerplate.com/Pages/Documents/Localization#extending-localization-sources

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because of no recent activity. Please create a new issue if you are still having this problem.