I have a WPF application that uses the ObservableValidator to handle validation of properties using data annotations. This works great for string properties. For example:
public class LoginViewModel : ObservableValidator
{
[ObservableProperty]
[NotifyDataErrorInfo]
[Required]
[StringLength(64, MinimumLength = 8)]
private string username = String.Empty;
[RelayCommand]
private void LogIn()
{
ValidateAllProperties();
if(HasErrors)
{
return;
}
// Log in the user
}
// ...
}
When I bind the Username property to a text box using <TextBox Text="{Binding Username, ValidatesOnNotifyDataErrors=True}" />, I automatically get validation messages in my view that appear around the textbox!
However, I don't know how I'm supposed to handle cases where I need to validate non-string properties. For example:
public class User : ObservableValidator
{
[ObservableProperty]
[NotifyDataErrorInfo]
[Required]
[Range(0, 200)]
private int age = 10;
}
If I used the same approach of <TextBox Text="{Binding Age, ValidatesOnNotifyDataErrors=True}" /> and the user enters a value that's not an integer like "12aaa", the default value converter on the binding throws an exception saying that "12aaa" cannot be converted into an integer. These value conversion exceptions can't be detected from my view model since the binding engine never updates the property value.
Thus, calling ValidateAllProperties() sets HasError to false, even though the user entered invalid data!
I see a few ways to handle these impossible-to-detect errors:
- Prevent the user from ever entering invalid data. This seems feasible at first, but becomes harder with more complicated types (e.g., a user input for a
TimeSpan). - Add string fields for each of the non-string properties. Even if you do this, I don't know how to propagate validation errors from the typed properties to the non-typed counterparts so they show up around the appropriate text box.
- Probably some other options I haven't thought of.
Are there any recommended ways of handling conversion errors for non-string properties with the MVVM Toolkit's ObservableValidator? Thanks in advance for your help!