MaterialDesignInXaml.Examples icon indicating copy to clipboard operation
MaterialDesignInXaml.Examples copied to clipboard

Unable to find a way to have this work with custom validations

Open Slagmoth opened this issue 2 years ago • 4 comments

Specifically, when you are in need of the ValidationContext in the IsValid overload for ValidationAttribute.

GetCustomAttributes only returns the one method for use.

Slagmoth avatar Apr 17 '23 17:04 Slagmoth

I am not sure I fully understand the question. ValidationAttribute.IsValid has two overloads, one of which takes in the ValidationContext as a method parameter. See: https://learn.microsoft.com/dotnet/api/system.componentmodel.dataannotations.validationattribute.isvalid So in many cases you should be able to simply override this overload instead.

As a better option take a look a the CommunityToolkit.Mvvm package. It comes with some nice APIs for doing validation. You can see the documentation for it here: https://learn.microsoft.com/dotnet/communitytoolkit/mvvm/observablevalidator

Keboo avatar Apr 19 '23 05:04 Keboo

It appears it is not an issue with the Abstract class itself but with Reflection, near as I can tell. When we tried to access the IsValid overload to utilize the ValidationContext it only shows the one method with only one parameter. So we had to call GetValidationResults instead within that overload. Sort of hacky but it worked(ish)

My shop tends to shy away from open source libraries, I realize that the microsoft solution is deprecated but we are on some seriously legacy stuff so this should work for now. We will revist when/if we ever get to rework this to API and CORE tech.

I am currently trying to find a way to extend you Abstract class to include IValidatableObject so that I can custom validate on multiple properties. Hoping this will alleviate the event conflicts I am getting. I have the need to show propA and/or propB as red if one or the other is of a specific value that violates invalid combinations.

Another concern is that I am getting a pretty hard performance hit on one custom validation but I am fairly certain this is just my LINQ logic being expensive.

Slagmoth avatar Apr 20 '23 15:04 Slagmoth

I am also having trouble displaying all current errors in the template, having trouble finding a primer on the parts of the template for display.

Slagmoth avatar Apr 20 '23 15:04 Slagmoth

So for some reason GetCustomAttributes will only provide the one overload.

image

Slagmoth avatar Apr 21 '23 14:04 Slagmoth