Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Possibility to change validation rules dymamically #71

Closed
ili opened this issue Apr 15, 2020 · 5 comments · Fixed by #128
Closed

feature: Possibility to change validation rules dymamically #71

ili opened this issue Apr 15, 2020 · 5 comments · Fixed by #128

Comments

@ili
Copy link

ili commented Apr 15, 2020

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");

   //...   
}
)
@open-collective-bot
Copy link

open-collective-bot bot commented Apr 15, 2020

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!

@glennawatson
Copy link
Collaborator

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.

@worldbeater
Copy link
Collaborator

worldbeater commented Apr 29, 2020

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

@worldbeater
Copy link
Collaborator

worldbeater commented Oct 13, 2020

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();

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants