Open Closed

Abp Dto Validation - Custom Validation Attributes #9159


User avatar
0
mgurer created

Hello, I have created a custom data annotition attribute. Below is the code;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
    AllowMultiple = false)]
public class PspRequiredAttribute : RequiredAttribute
{
    public PspRequiredAttribute()
    {
        ErrorMessageResourceName = nameof(PspResource.RequiredAttribute_ValidationError);
        ErrorMessageResourceType = typeof(PspResource);
    }
}

Here is a sample create dto for testing;

public class ProductCreateDto
{
    [PspRequired]
    [PspStringLength(ProductConsts.NameMaxLength)]
    [Display(Name = "Product_Name", ResourceType = typeof(ProductServiceResource))]
    public string Name { get; set; }
}

On blazor pages, after applying new PspRequired attribute, blazor forms automatically gets localized for validation messages. Otherwise, using classic Required attribute, blazor ui localizations fails, and only english localizations are shown for models ui validation messages.

But when I send this dto to create method of service, I get an error of this type: System.InvalidOperationException, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e

When I use standard Required attribute, no error occcurs.

I placed breakpoints on conrollers create action but no breakpoint hits but error is returned. I can not figure out what is going on because no breakpoint hits.

How can I use my own anotation attributes as in the example.

Tnx Murat


2 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    But when I send this dto to create method of service, I get an error of this type: System.InvalidOperationException, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e

    Hi Murat, API is using the default .NET model binder, and something in your custom attribute is causing a validation metadata exception or reflection issue before the controller is even entered.

    There are some checks that you can do:

    • Make sure PspResource is in a shared project. (It typically should be in *.Domain.Shared project)
    • Comment out the constructor of your attribute and debug your application to see if the related 2 lines are the problem:
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
        AllowMultiple = false)]
    public class PspRequiredAttribute : RequiredAttribute
    {
        public PspRequiredAttribute()
        {
            //ErrorMessageResourceName = nameof(PspResource.RequiredAttribute_ValidationError);
            //ErrorMessageResourceType = typeof(PspResource);
        }
    }
    

    When your API (the backend part) tries to reflect over the DTO, it's trying to fetch the error message string via the ErrorMessageResourceType during model binding, and probably this where you get error.


    After you have tried these two suggestions, please let me know about the situation. If it's not related to reflection problem, then please share your PspResource class and give more details (such as where the resource class is defined, where you created the attribute, and any other context)

    Regards.

  • User Avatar
    0
    mgurer created

    Well my problem was about not being able to debug the validation process. But your suggestion gave me a hint. I added following line to my validator and problem solved.

    public override string FormatErrorMessage(string name)
    {
        return string.Format(PspResource.RequiredAttribute_ValidationError, name);
    }    
    

    Thanks.

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.3.0-preview. Updated on May 12, 2025, 05:22