Jason Towne
Jason Towne

Reputation: 8050

ReactiveUI and Validation

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:

  1. If SendEmail == true then 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.)
  2. If a value was set to the Email property, make sure it's a valid email address.
  3. If either 1. or 2. fail validation, SaveMyDataCommand should 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?

Upvotes: 1

Views: 1398

Answers (2)

Jason Towne
Jason Towne

Reputation: 8050

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 to System.ComponentModel to your class as well.

public class MyViewModel: ReactiveValidatedObject
{
  public ReactiveAsyncCommand SaveMyDataCommand { get; protected set; }

  // THIS PROPERTY INDICATES WHETHER ALL FIELDS HAVE BEEN VALIDATED
  public bool IsSaveEnabled
  {
    get { return IsObjectValid(); }
  }

  private string _email;
  [ValidatesViaMethod(AllowBlanks=true,AllowNull=true,Name="IsEmailValid",ErrorMessage="Please enter a valid email address")]
  public string Email
  {
    get { return _email; }
    set
    {
      _email = value;
      raisePropertyChanged("Email");
      SendEmail = SendEmail;
      raisePropertyChanged("IsSaveEnabled");
    }
  }

  private string _name;
  public string Name
  {
    get { return _name; }
    set
    {
      _name= value;
      raisePropertyChanged("Name");
    }
  }

  private bool _sendEmail = false;
  [ValidatesViaMethod(Name = "IsSendEmailValid", ErrorMessage = "Make sure a valid email address is entered.")]
  public bool SendEmail
  {
    get { return _sendEmail; }
    set
    {
      _sendEmail = value;
      raisePropertyChanged("SendEmail");
      raisePropertyChanged("IsSaveEnabled");
    }
  }  

  public bool IsEmailValid(string email)
  {
    if (string.IsNullOrEmpty(email))
    {
      return true;
    } 

    // Return result of Email validation
  }

  public bool IsSendEmailValid(bool sendEmail)
  {
     if (sendEmail)
     {
       if (string.IsNullOrEmpty(Email))
       {
         return false;
       }

       // Return result of Email validation
     }
  }

  public MyViewModel()
  {
    SaveMyDataCommand = new ReactiveAsyncCommand(null, 1);
  }
}

I hope this helps someone! :)

Upvotes: 3

Ana Betts
Ana Betts

Reputation: 74652

Use ReactiveValidatedObject, then use Data Annotations (on phone, sorry for short message)

Upvotes: 1

Related Questions