SignUpViewModel.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Reactive;
  2. using ReactiveUI;
  3. using ReactiveUI.Validation.Extensions;
  4. using ReactiveUI.Validation.Helpers;
  5. namespace Generators.Sandbox.ViewModels;
  6. public class SignUpViewModel : ReactiveValidationObject
  7. {
  8. private string _userName = string.Empty;
  9. private string _password = string.Empty;
  10. private string _confirmPassword = string.Empty;
  11. public SignUpViewModel()
  12. {
  13. this.ValidationRule(
  14. vm => vm.UserName,
  15. name => !string.IsNullOrWhiteSpace(name),
  16. "UserName is required.");
  17. this.ValidationRule(
  18. vm => vm.Password,
  19. password => !string.IsNullOrWhiteSpace(password),
  20. "Password is required.");
  21. this.ValidationRule(
  22. vm => vm.Password,
  23. password => password?.Length > 2,
  24. password => $"Password should be longer, current length: {password.Length}");
  25. this.ValidationRule(
  26. vm => vm.ConfirmPassword,
  27. confirmation => !string.IsNullOrWhiteSpace(confirmation),
  28. "Confirm password field is required.");
  29. var passwordsObservable =
  30. this.WhenAnyValue(
  31. x => x.Password,
  32. x => x.ConfirmPassword,
  33. (password, confirmation) =>
  34. password == confirmation);
  35. this.ValidationRule(
  36. vm => vm.ConfirmPassword,
  37. passwordsObservable,
  38. "Passwords must match.");
  39. SignUp = ReactiveCommand.Create(() => {}, this.IsValid());
  40. }
  41. public ReactiveCommand<Unit, Unit> SignUp { get; }
  42. public string UserName
  43. {
  44. get => _userName;
  45. set => this.RaiseAndSetIfChanged(ref _userName, value);
  46. }
  47. public string Password
  48. {
  49. get => _password;
  50. set => this.RaiseAndSetIfChanged(ref _password, value);
  51. }
  52. public string ConfirmPassword
  53. {
  54. get => _confirmPassword;
  55. set => this.RaiseAndSetIfChanged(ref _confirmPassword, value);
  56. }
  57. }