Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Slagmoth opened this issue Apr 17, 2023 · 4 comments
Open

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

Slagmoth opened this issue Apr 17, 2023 · 4 comments
Labels

Comments

@Slagmoth
Copy link

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

GetCustomAttributes only returns the one method for use.

@Keboo
Copy link
Owner

Keboo commented Apr 19, 2023

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 Keboo added the question label Apr 19, 2023
@Slagmoth
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants