✅ ValidationContext in ASP.NET
What does ObjectInstance and ObjectType mean? afaik
1) ValidationContext is basically on which class the validation is performed.
2) ObjectInstance is where we have all the values for a model class
3) I'm not sure what object type is.
What does this code do here?
I'm trying to fetch a different property value from in the same model class
Model class
16 Replies
ObjectType would refer to the classtype so as you described 'Model Class'
so ObjectType is basically the T in
Type T = Type.GetType("Model Class")
?Yeh essentially
what does validationcontext point to in this context?
Think of it as like the context of what's being validated. Look at my recent code for example:
It provides you access to the context of that validation that's being run (Sorry if this is a bad explanation). I can now view all the properties on my model and see their values etc. Hence why you use it to get the value of whatever property you're validating
ohkayy, let me tell you what i understood
let's say i have a property called name and i need to validate it
so the validationcontext basically has info regarding property called name right? like it's display name and membername etc..
now the validationcontext also has 2 more properties called ObjectType and ObjectInstance and we use objecttype to see all the properties/methods in the class and if needed we can get the reference of a single property as well
now to see the actual value of the a property which we fetched using objectype, we need to use object instance?
otherproperty.GetValue(validationContext.ObjectInstance)
what does this code mean? why can't we just otherproperty.GetValue("propertyname")
do this?
alsoo, how are you directly accessing the value from ObjectInstance? is that because you casted it? var model = (PriorityServiceRegister)validationContext.ObjectInstance
Sounds like you're beginning to understand it.
Do you have the whole method?
In theory Convert.ToDateTime(otherproperty.GetValue(validationContext.ObjectInstance)); is redundant as if you look at my above the Override for ValidationResult passes the value in directly.
exactly, i have the whole method, let me put it in here
You can use validationContext if you want to gain a more granular access to the context of validation.
For exmple your above could literally just be this:
casting your objectinstace to your model is easy and convenient like you did
Above is pseudocode but you get the picture 🙂
yes yes, i just have 1 more doubt regarding this piece of code
otherproperty.GetValue(validationContext.ObjectInstance)
otherproperty basically has info about the property we want from the objectinstance
so does that piece of code mean, get me the value of "otherproperty" from validationcontext.objectinstance?
this is the whole code
trying to fetch from date and the validationattribute is on to date property
thisvar model = (User)validationContext.ObjectInstance;
Exactly, the above now gives you access to your whole model and it's properties.
Something to consider though:
1. Do you really want to just apply the validation to one out out of the two properties?
Why? Well errors/feedback, you may want to have 1 error on the FromDate and another on the TooDate that feedbacks to the user
yes, sir. I'm currently learning this but i believe giving 2 different error from 2 properties sounds independent and i think that's how it should be
Are the dates really nullable if you're requiring dates?
ideally they should not be nullable, like i was saying i'm just learning as of now following tutorials and stuff
but thanks man! I feel like i really understood this stuff well enough to explain it back to someone
I'm glad i had this convo