-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[Feature] Manually adding errors to ObservableValidator #3750
Comments
Hello, 'sherman89! Thanks for submitting a new feature request. I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future! |
@Sergio0694 you just added some helpers for this in the latest PR, eh? I think that was from #3742, #3729, and #3665, eh? |
Hey @sherman89, thank you for trying it out, really happy to hear you're enjoying it so far! 😄 I believe there's an easy way to achieve what you're asking for without making modifications to the API surface, in particular I think it would be best to avoid adding API to let consumers manually change the internal state of the validator like that, since the validation APIs are actually meant to be used through the What you're asking can actually be done already in two different ways, here's one with the public class ValidationWithServiceModel : ObservableValidator
{
private readonly IFancyService service;
public ValidationWithServiceModel(IFancyService service)
{
this.service = service;
}
private string name;
[MaxLength(25, ErrorMessage = "The name is too long")]
[CustomValidation(typeof(ValidationWithServiceModel), nameof(ValidateName))]
public string Name
{
get => this.name;
set => SetProperty(ref this.name, value, true);
}
public static ValidationResult ValidateName(string name, ValidationContext context)
{
bool isValid = ((ValidationWithServiceModel)context.ObjectInstance).service.Validate(name);
if (isValid)
{
return ValidationResult.Success;
}
return new ValidationResult("The name contains invalid characters");
}
} You can see here the I've added some unit tests in fc39f6f to showcase and test this scenario, let me know if that helps and if this solution works for you! Alternatively you could also pass a custom validation context with an |
@Sergio0694 Thank you Sergio that's exactly what I needed! Just tested it and works great! I had no idea Thank you very much for the quick response as well, I posted the issue and went to sleep, didn't expect to get an answer so quick 😄 Will close this issue now :) Best regards! |
Thank you for this library first of all, really enjoying it so far! ❤️
Is it possible to add
AddError
andClearErrors
methods (or something similar) toObservableValidator
for custom validation? In my case it's not possible to use validation attributes since my validation involves calling an injected service that does complicated validation logic.Forgive me if there's already a way to do this, but I just couldn't figure it out 😕
Describe the problem this feature would solve
It would allow for more complex validation scenarios where a validation attribute is not enough. For example:
Describe the solution
A way to manually add/clear errors to the error collection manually. It would help in cases where it's not possible or elegant to use validation attributes.
Describe alternatives you've considered
I tried creating a custom attribute and using the
ValidationContext
somehow to access my service, but decided in the end that it's way too ugly and hacky, and not a very reusable solution. I've also considered simply not using the toolkits implementation ofINotifyDataErrorInfo
, but it would be really nice if I didn't have to do that.Additional context & Screenshots
None.
The text was updated successfully, but these errors were encountered: