How do you implement INotifyDataErrorInfo for robust validation?
Short answer
Implement INotifyDataErrorInfo on the view model to expose per-property errors and raise ErrorsChanged. It supports async and cross-field validation and integrates with binding so the UI can reflect HasErrors and per-field messages.
Advertisement
AModel answer
Implement INotifyDataErrorInfo on the view model to expose per-property errors and raise ErrorsChanged. It supports async and cross-field validation and integrates with binding so the UI can reflect HasErrors and per-field messages. The MVVM Toolkit's ObservableValidator provides an attribute-driven implementation.
public partial class RegisterVm : ObservableValidator
{
[ObservableProperty]
[Required, EmailAddress]
string email;
// ValidateAllProperties() populates errors for binding
}Real-world example
A form leverages DataAnnotations via ObservableValidator so [Required]/[EmailAddress] drive both validation and error display.
Likely follow-ups
- What does ObservableValidator add? | Can it do cross-field rules?
Related topics