Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which IsValid method should I override when implementing custom ValidationAttribute

When I implement a custom attribute inherited from ValidationAttribute class, I've always overrided bool IsValid(object value) method disgarding the other one whose prototype is ValidationResult IsValid(objet value, ValidationContext validationContext).

Maybe, I should override the second method instead even if I don't use validation context or the result (I use validation with EntityFramework and ModelState.IsValid controller property). Or continue to disregard the overloaded method. If then, could I have an object valid or invalid depending on the context the attribute validation is invoked? Is a situation as shown in the code bellow problematic?

class StrictlyPreviousAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var dateTime = value as DateTime?;
        return dateTime == null || dateTime <= DateTime.Today;
    }
}

class PreviousAttribute : StrictlyPreviousAttribute 
{
    public override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var dateTime = value as DateTime?;

        if(dateTime == DateTime.Today)
        {
            return ValidationResult.Success;
        }
        else
        {
            return base.IsValid(object);
        }
    }
}

I don't know if there is there object misconception or if I'm missing some point. Is there a method I should preferably override instead of the other. Should I override both.

like image 392
Francois Stock Avatar asked Aug 09 '16 18:08

Francois Stock


People also ask

Which class should be derived from to implement custom validation on entities?

ValidationAttribute is a class in the DataAnnotation namespace that allows the creation of a custom attribute depending on the needs of the developer to validate the entity. The following is an implementation of the custom validation attribute: [AttributeUsage(AttributeTargets. Property, AllowMultiple = false)]

What is IsValid?

The IsValid method confirms that the interface of a DCO object is valid and is connected to an actual object.

What interface is used for custom ASP NET core client side validation?

Implementing IClientModelValidator interface This method sets certain client side custom data attributes or data-* attributes that are used by the validation system. Specifically we set the data-val and data-val-country attributes. The later data attribute specifies the client side error message.

What is custom validation in MVC?

This validation can be added for both the client side and the server side. You understand that decorating the properties in a model with an Attribute can make that property eligible for Validation. Some of the DataAnnotation used for validation are given below. Required. Specify a property as required.


1 Answers

The link provided by Steve Greene in the comment above enlighten the difference between the methods and answer the question.

The method returning a bool still exist for backward compatibility but is not abstract anymore since .NET 4.0. It is advised to override the method that have access to the ValidationContext.

This (amongst other things) gives us access to the whole model even when the validator is at the property level. The massive advantage of using a property level validator is that the error is set against the property itself instead of the class removing the requirement to use an Html.ValidationSummary. Since the validation error is set against the property correctly, your normal Html.ValidationFor Html.ValidationMessageFor helpers will pick up and display the error against the invalid form field.

It has been written a while ago (with ASP.NET MVC 3) but remains valid with .NET 4.6 framework. Also, I implemented unit tests for the bool IsValid(object value) method. The tests continue to pass even if I only override the method with the ValidationContext parameter.

like image 89
Francois Stock Avatar answered Sep 28 '22 11:09

Francois Stock