reactiveui / ReactiveUI.Validation

Validation helpers for ReactiveUI-based apps.

Home Page:https://reactiveui.net/docs/handbook/user-input-validation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feature: Possibility to change validation rules dymamically

ili opened this issue · comments

Hello, firstly thanks for your great project.

It would be great if validation would support dynamic validation rules change: for example, i do deed to configure some hardware, different options are available for different models, for ex. "Model A" can be connected either via TCP/IP or via RS232, "Model B" supports RS232 only, so for the first TCP/IP settings required, for the second - RS232.

So i do need smth like:

this.WhenAnyValue(_ => _.Model)
.Subscribe(m => 
{
    this.ClearValidationRules(_ => _.IpAddress);
    this.ClearValidationRules(_ => _.Rs232Port);
   //...

  if (m == Model.A)
      this.ValidationRule(_ => _.IpAddress, _ => !string.IsNullOrEmpty(_), "IP address required");

   //...   
}
)

Hey @ili 👋,

Thank you for opening an issue. We will get back to you as soon as we can. Also, check out our Open Collective and consider contributing financially.

https://opencollective.com/reactiveui

PS.: We offer priority support for all financial contributors. Don't forget to add priority label once you start contributing 😄

An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms!
commented

I think the validation rules are stored in a dictionary at the moment so should be possible. I got some other stuff happening at the moment so wouldn't be able to immediately get to it. But reasonable request.

Currently, you can achieve the described behavior by doing the following:

// Listen to Model and IpAddress changes and
// require IpAddress property to not be empty 
// only if Model equals to Models.A.
var ipAddressRule = this
    .WhenAnyValue(x => x.IpAddress)
    .CombineLatest(
        this.WhenAnyValue(x => x.Model), 
        (ipAddress, model) => 
            model == Models.A && 
            !string.IsNullOrEmpty(ipAddress) ||
            model == Models.B && ...);

this.ValidationRule(
    x => x.IpAddress, 
    ipAddressRule, 
    "IP address required.");

The code is based on a new feature introduced in #58

Now it should be possible to remove validation rules via a call to DisposeWith. E.g. the following code will attach an IValidationComponent to an IValidatableViewModel when we call this.ValidationRule(), and will detach the IValidationComponent when we dispose of the cleanup disposable:

var cleanup = new CompositeDisposable();

// Attach a validation rule to the IValidatableViewModel.
this.ValidationRule(
    viewModel => viewModel.Name,
    name => string.IsNullOrWhiteSpace(name),
    "Name shouldn't be empty.")
    .DisposeWith(cleanup);

// Detach and dispose the validation rule.
cleanup.Dispose();

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.