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.
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)]
The IsValid method confirms that the interface of a DCO object is valid and is connected to an actual object.
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.
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.
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.ValidationForHtml.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With