Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Existing Data Annotation Attribute in asp.net core 1.1

I am trying to override the RequiredAttribute in .net core and does not seem to work on asp.net core 1.1

Here is the test code

public class CustomRequiredAttribute : RequiredAttribute
{
    public CustomRequiredAttribute():base()
    {

    }

    public override string FormatErrorMessage(string name)
    {
        return base.FormatErrorMessage(name);
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        return base.IsValid(value, validationContext);
    }

}

Once used on my model I am expecting the normal result like field is required as I have not customized it yet and just calling base methods.

This does not seem to work as expected and just bypasses the required on both the client and server side.

The purpose of this is to add a validation message pulled from a db to the ErrorMessage property.

like image 207
AliK Avatar asked Dec 23 '16 04:12

AliK


People also ask

What is Modelstate in asp net core?

Model state represents errors that come from two subsystems: model binding and model validation. Errors that originate from model binding are generally data conversion errors. For example, an "x" is entered in an integer field.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

What is range attribute?

The range attribute allows a numeric range to be specified for a slot when a numeric value is used in that slot. If a numeric value is not used in that slot, then no checking is performed.

How to use required attribute in c#?

The Required attribute indicates that a property must have a value; in this sample, a movie has to have values for the Title , ReleaseDate , Genre , and Price properties in order to be valid. The Range attribute constrains a value to within a specified range.


1 Answers

Your problem is that the ValidationAttributeAdapterProvider, which is the default implementation of IValidationAttributeAdapterProvider, checks for specific types only. Thus, using custom implementations leads to missing "adapter providers", which leads to missing data attributes.

Solution: provide your own implementation of IValidationAttributeAdapterProvider, which can forward to the default implementation for non custom stuff...

public class CustomValidationAttributeAdapterProvider : IValidationAttributeAdapterProvider
{
    private IValidationAttributeAdapterProvider innerProvider = new ValidationAttributeAdapterProvider();

    public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
    {
        if (attribute == null)
            throw new ArgumentNullException(nameof(attribute));

        var type = attribute.GetType();

        if (type == typeof(CustomRequiredAttribute))
            return new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);

        return innerProvider.GetAttributeAdapter(attribute, stringLocalizer);
    }
}

...and register it as a singleton.

services.AddSingleton<IValidationAttributeAdapterProvider, CustomValidationAttributeAdapterProvider>();
like image 149
Axel Heer Avatar answered Sep 30 '22 03:09

Axel Heer