L
L
Lapish722020-08-06 01:57:44
WPF
Lapish72, 2020-08-06 01:57:44

Validation with ReactiveUI.Validation?

Goodnight! I'm building a client-server application for myself and ran into a validation issue with ReactiveUI.Validation. Namely, the problem is that if the ViewModel has one property, then the validation works, but after 2 property changes. As soon as I add the 2nd property, everything works fine.

I read the documentation, found 1 example and created a test project.

MainWindowViewModel.cs

public class MainWindowViewModel : ReactiveValidationObject<MainWindowViewModel>
{
    [Reactive]
    public string FirstName { get; set; } = string.Empty;

    public MainWindowViewModel()
    {
        this.ValidationRule(viewmodel => viewmodel.FirstName,
            fs => !fs.Contains(" "),
            "Не может быть пробелов");

        this.ValidationRule(viewModel => viewModel.FirstName,
            firstName => !string.IsNullOrWhiteSpace(firstName), 
            "Имя не может быть пустым");
    }
}


MainWindow.xaml
<reactiveui:ReactiveWindow 
        x:Class="ReactiveValidation.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:vms="clr-namespace:ReactiveValidation.ViewModels"
        x:TypeArguments="vms:MainWindowViewModel"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:reactiveui="http://reactiveui.net"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525" >

    <StackPanel>
        <TextBox Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                 md:HintAssist.Hint="Имя" Style="{DynamicResource MaterialDesignFloatingHintTextBox}" Margin="30"/>
    </StackPanel>
</reactiveui:ReactiveWindow>


MainWindow.xaml.cs
public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
{
    public MainWindow()
    {
        InitializeComponent();
        ViewModel = new MainWindowViewModel();
        this.WhenActivated(disposableRegistration =>
        {
        });
    }
}


App.xaml.cs
public partial class App
{
    protected override Window CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {

    }
}


Validation works, but after 2 steps. For example, I enter "1", "2", " " and no error will appear. But, if I enter any character now, an error will immediately appear. Why is this happening?

However, if I add another property, which is not related to the View in any way, but to the ValidationRule property, then everything works wonderfully. That is, after each property change, the check goes immediately, and not after 2 changes.

Magic
public class MainWindowViewModel : ReactiveValidationObject<MainWindowViewModel>
{
    [Reactive]
    public string FirstName { get; set; } = string.Empty;

    [Reactive]
    public string Test { get; set; }

    public MainWindowViewModel()
    {
        this.ValidationRule(viewmodel => viewmodel.FirstName,
            fs => !fs.Contains(" "),
            "Не может быть пробелов");

        this.ValidationRule(viewModel => viewModel.FirstName,
            firstName => !string.IsNullOrWhiteSpace(firstName), 
            "Имя не может быть пустым");

        this.ValidationRule(
            vm => vm.Test,
            test => !string.IsNullOrWhiteSpace(test),
            "567");
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question