Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of ValidationContext when implementing IValidatableObject

I have implemented IValidatableObject several times and have never found out what the purpose of parsing ValidationContext to the Validate method is - my typical IValidatableObject implementation looks something like this:

 public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
 {
    if (Prop1 == Prop2)
    {
        yield return new ValidationResult(
              "Prop1 and Prop2 must be different.",
              new[] {"Prop1", "Prop2"});
    }
 }

Is there anything that I have missed that I could have used validationContext for?

EDIT: I'm using ASP.NET MVC and this is implemented in the class - not in the controller.

like image 761
Henrik Stenbæk Avatar asked Mar 19 '13 12:03

Henrik Stenbæk


People also ask

What is ValidationContext?

Interface ValidationContextA context for a validator to use to access user data and report validation failures.

When validate is called on an object that implements IValidatableObject what is returned from that call?

When you click on "Implement interface 'IValidatableObject'" you will have the following interface. This will return IEnumerable<ValidationResult> by taking one parameter as ValidationContext.

What is the purpose of the validationcontext class?

This class describes the type or member on which validation is performed. It also enables custom validation to be added through any service that implements the IServiceProvider interface. Initializes a new instance of the ValidationContext class using the specified object instance.

What is the difference between ivalidatableobject and other validations?

Other validations will be executed eventually only if [Required] validation has been passed. And IValidatableObject takes the lowest priority. It’s going to be called only if there’s no [Required] error and no other errors like [Range].

How to implement custom validation in Salesforce?

This validation can be implemented using the custom validation attribute as shown in below code. Another option to apply custom validations is to implement interface – IValidatableObject – in model class. This interface provides a method – Validate – which can be implemented in the class.

How do I use custom validation in validationattribute?

ValidationAttribute is an abstract class and it is used as base class for all the built-in validation attributes. If a custom validation attribute is required, create a new class which extends ValidationAttribute. Next, override the abstract method – IsValid – to implement custom validation logic.


2 Answers

ValidationContext contains IServiceProvider property. It is extension point to pass DI container to your validation attributes and Validate methods. You can use it, as example, to validate against database without setting dependency on dbcontext in your model.

like image 55
Kirill Bestemyanov Avatar answered Oct 10 '22 02:10

Kirill Bestemyanov


You should retrieve Prop1 and Prop2 from validationContext. From your question it's difficult to say whether you are using WebForms (so you have code-binding with properties) or MVC (and if you are implementing this in the controller).

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
 {
    if (validationContext["Prop1"] == validationContext["Prop2"])
    {
        yield return new ValidationResult(
              "Prop1 and Prop2 must be different.",
              new[] {"Prop1", "Prop2"});
    }
 }
like image 6
usr-local-ΕΨΗΕΛΩΝ Avatar answered Oct 10 '22 01:10

usr-local-ΕΨΗΕΛΩΝ