What’s considered “best practice” when performing data validation while using ReactiveUI? Let’s say I have a view model that looks like this:
public class MyViewModel: ReactiveObject
{
public ReactiveAsyncCommand SaveMyDataCommand { get; protected set; }
private string _email;
public string Email
{
get { return _email; }
set
{
_email = value;
raisePropertyChanged("Email");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name= value;
raisePropertyChanged("Name");
}
}
private bool _sendEmail = false;
public bool SendEmail
{
get { return _sendEmail; }
set
{
_sendEmail = value;
raisePropertyChanged("SendEmail");
}
}
public MyViewModel()
{
SaveMyDataCommand = new ReactiveAsyncCommand(null, 1);
}
}
Here’s what I want to validate:
- If
SendEmail == truethen make sure there’s a valid email address in the Email property. (I’m not worried about the actual email address validation piece itself. This is just a what if scenario.) - If a value was set to the
Emailproperty, make sure it’s a valid email address. - If either 1. or 2. fail validation,
SaveMyDataCommandshould not be executable.
I’m just looking for a good example on how to do simple / slightly more complex data validation using ReactiveUI. Can anyone shed some light on this?
For anyone else looking for an example on using
ReactiveValidatedObject, here’s what worked for me. Note that you’ll have to add a reference toSystem.ComponentModelto your class as well.I hope this helps someone! 🙂