C
C#15mo ago
Slagmoth

Custom attribute for ValidationAttribute, am I able to get the property being validated dynamically?

Looking for a way to have a custom validation attribute apply to two related properties... simple solution is that I just make a reversed validation for the related property but would like it cleaner if possible. So I have two dropdowns in a grid that are interdependent... so if one says "Direct" the other can't have a value of "A". Both are validated off the same stuff all I would like to be able to do is just pass which one is being validated to the attribute. So is it possible to get which property is being called dynamically from either object value or ValidationContext validationContext in the parameters or through reflection?
3 Replies
JakenVeina
JakenVeina15mo ago
the better bet is to rethink your problem or, at least, think about it the way the designers of this API did validation rules that require looking at two properties upon an object are not validation rules that apply to one of those properties they are validation rules that apply to the whole object write your custom validation attribute to be attached to the whole object, and it has all the properties available to it and it can still return a result that says "Property XYZ is invalid"
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Slagmoth
Slagmoth15mo ago
Thanks for the reply, we have been playing with IDataErrorInfo and other things... I was able to figure out a solution for the properties in question at least. I also found how to pass the name of the property through the constructor so that helps for more dynamic things later. We tried whole row validations but none of them fired correctly. The way this was initially designed didn't have any forward thinking or extensibility in mind... none of the built in stuff from WPF datagrids were used either which would have made things a tad easier. RowValdation just didn't play nice and it would not have been viable to redesign it from scratch to get it done... one of my gripes with AGILE and MVP 😦