ReactiveUI 和验证

发布于 2024-11-30 05:59:29 字数 1246 浏览 3 评论 0原文

使用 ReactiveUI 执行数据验证时什么被认为是“最佳实践”?假设我有一个如下所示的视图模型:

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

我只是在寻找一个关于如何使用 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);
    }
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度° 2024-12-07 05:59:29

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

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

我希望这对某人有帮助! :)

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

梦在深巷 2024-12-07 05:59:29

使用ReactiveValidatedObject,然后使用Data Annotations(在电话上,抱歉短信)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文