7

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.

8
  • You are not overriding it, you are creating a derived class. Can you show how you are using it? Commented Dec 28, 2016 at 6:37
  • I am deriving and once it works will override certain methods, i am using it as standard on the model i.e [CustomRequired] public string Name{get;set;} I was hoping as intented it would call the isvalid but breakpoint never hit. Commented Dec 28, 2016 at 6:55
  • 1
    See if this SO helps: stackoverflow.com/questions/12573362/… Commented Dec 28, 2016 at 7:03
  • Not really as I am using .net core and the it seems DataAnnotationsModelValidatorProvider does not exist in any of the libraries. Commented Dec 28, 2016 at 7:11
  • When you look at your html, do you see the data-val-required attribute? Commented Dec 28, 2016 at 7:19

1 Answer 1

3

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>();
Sign up to request clarification or add additional context in comments.

3 Comments

I have tired something similar and I believe this is the right direction to go.
@AliK It actually works. Or did I miss to mention some important step?
I followed a similar pattern but yes this should work also. The problem is at this point as you know the framework keeps changing so for now its fine lets see what happens with the new releases from MS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.