ReactiveUI和验证 [英] ReactiveUI and Validation

查看:167
本文介绍了ReactiveUI和验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ReactiveUI进行数据验证时,什么被认为是最佳实践"?假设我有一个看起来像这样的视图模型:

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);
    }
}

这是我要验证的内容:

  1. 如果为SendEmail == true,请确保在电子邮件"属性中有一个有效的电子邮件地址. (我不担心实际的电子邮件地址验证部分本身.这只是一个假设场景.)
  2. 如果将值设置为Email属性,请确保它是有效的电子邮件地址.
  3. 如果1.或2.验证失败,则SaveMyDataCommand不应执行.
  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.

我只是在寻找一个很好的示例,说明如何使用ReactiveUI进行简单/稍微复杂的数据验证.谁能对此有所启发?

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?

推荐答案

对于其他正在寻找使用ReactiveValidatedObject的示例的人,这对我有用.请注意,您还必须在类中添加对System.ComponentModel的引用.

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! :)

这篇关于ReactiveUI和验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆