ReactiveUI仅在用户输入更改时进行验证



我是ReactiveUI的新手,所以我坚持这个问题,我的错误消息显示在页面开始处,而不是在用户输入更改之后。

我想我对ComplexRule有意见。下面是我在ViewModel中的代码:

public LoginViewModel(IScreen screen)
{
HostScreen = screen;
NameRule = this.ValidationRule(
viewModel => viewModel.UserName,
name => name.Length > 2,
"You must specify a valid name longer then 2 sybols.");
//checks with Regexp password
PasswordRule = this.ValidationRule(
viewModel => viewModel.Password,
password => PasswordValidator.Validate(password),
"You must specify a valid password longer then 8 sybols with at least 1 digit, upper 
case, lower case and special characters.");
var nameAndPasswordRules = this
.WhenAnyValue(
x => x.UserName,
x => x.Password,
(name, password) => NameRule.IsValid && PasswordRule.IsValid);
ComplexRule = this.ValidationRule(
_ => nameAndPasswordRules,
(vm, state) => !state ? "Username and Password should be both valid!" : string.Empty);
var canNavigate = this.IsValid();
NavigateToMainPage = ReactiveCommand.CreateFromObservable(() =>
{
return HostScreen.Router.NavigateAndReset.Execute(new MyTabbedViewModel(HostScreen));
}, canNavigate);
}

这是我的观点:

public LoginView()
{
InitializeComponent();
this.WhenActivated(disposables =>
{
this.Bind(ViewModel, vm => vm.UserName, v => v.username.Text)
.DisposeWith(disposables);
this.BindValidation(ViewModel, vm => vm.NameRule, v => v.usernameErrors.Text)
.DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.Password, v => v.password.Text)
.DisposeWith(disposables);
this.BindValidation(ViewModel, vm => vm.PasswordRule, v => v.passwordErrors.Text)
.DisposeWith(disposables);
this.BindCommand(ViewModel, x => x.NavigateToMainPage, x => x.loginButton)
.DisposeWith(disposables);
});
}

因此,我试图在用户输入更改(密码/用户名(后实现验证,并显示适当的消息。我从文档中取了这个例子。

我尝试了几种方法,并完成了这一种。不确定这是否是最好的方法,但它有效(如果你知道如何做得更好,我想听听你的意见。

我将null值分配给属性

[Reactive]
public string UserName { get; set; } = null;
[Reactive]
public string Password { get; set; } = null;

所以我们假设null表示它从未被使用过。

并在ValidationRule 上进行验证

var name = this
.WhenAnyValue(x => x.UserName)
.Select(x => x == null || x?.Length > 2);
var password = this
.WhenAnyValue(x => x.Password)
.Select(x => x == null || PasswordValidator.Validate(x));
var nameAndPasswordRules = this
.WhenAnyValue(
x => x.UserName,
x => x.Password)
.DefaultIfEmpty()
.DistinctUntilChanged()
.SkipWhile(item => !string.IsNullOrWhiteSpace(item.Item1) && !string.IsNullOrWhiteSpace(item.Item2))
.Select(prop1 =>
!string.IsNullOrEmpty(prop1.Item1)
&& this.NameRule.IsValid
&& !string.IsNullOrEmpty(prop1.Item2)
&& this.PasswordRule.IsValid);
NameRule = this.ValidationRule(
_ => name,
(vm, state) => !state ? "You must specify a valid name longer then 2 sybols." : string.Empty);
PasswordRule = this.ValidationRule(
_ => password,
(vm, state) => !state ? "You must specify a valid password longer then 8 sybols with at least 1 digit, upper case, lower case and special characters." : string.Empty);
ComplexRule = this.ValidationRule(
_ => nameAndPasswordRules,
(vm, state) => !state ? "Username and Password should be both valid!" : string.Empty);

正如你所看到的,我做x == null || ...意味着绕过第一次尝试,因为用户还没有编辑条目

相关内容

  • 没有找到相关文章

最新更新