A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

81 lines
2.1 KiB

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Generators.Sandbox.ViewModels;
public class SignUpViewModel : ObservableValidator
{
private string? _userName;
private string? _password;
private string? _confirmPassword;
public SignUpViewModel()
{
UserName = "Joseph!";
Password = "1234";
ConfirmPassword = "1234";
SignUp = new RelayCommand(() => { }, () => !HasErrors);
ErrorsChanged += OnErrorsChanged;
}
public RelayCommand SignUp { get; }
[Required]
public string? UserName
{
get => _userName;
set => SetProperty(ref _userName, value, validate: true);
}
public string? UserNameValidation
=> GetValidationMessage(nameof(UserName));
[Required]
[MinLength(2)]
public string? Password
{
get => _password;
set
{
if (SetProperty(ref _password, value, validate: true))
ValidateProperty(ConfirmPassword, nameof(ConfirmPassword));
}
}
public string? PasswordValidation
=> GetValidationMessage(nameof(Password));
[Required]
[Compare(nameof(Password))]
public string? ConfirmPassword
{
get => _confirmPassword;
set => SetProperty(ref _confirmPassword, value, validate: true);
}
public string? ConfirmPasswordValidation
=> GetValidationMessage(nameof(ConfirmPassword));
public string? CompoundValidation
=> GetValidationMessage(null);
private void OnErrorsChanged(object? sender, DataErrorsChangedEventArgs e)
{
if (e.PropertyName is not null)
OnPropertyChanged(e.PropertyName + "Validation");
OnPropertyChanged(CompoundValidation);
SignUp.NotifyCanExecuteChanged();
}
private string? GetValidationMessage(string? propertyName)
{
var message = string.Join(Environment.NewLine, GetErrors(propertyName).Select(v => v.ErrorMessage));
return string.IsNullOrEmpty(message) ? null : message;
}
}