Knockout-Validation icon indicating copy to clipboard operation
Knockout-Validation copied to clipboard

Enable/disable validation rules

Open KurtJanssen opened this issue 14 years ago • 5 comments

I ran into the need of enabling/disabling validation rules dynamically. Is it possible to add 'enabled' to the context of a validation rule. When disabled the validator could simply return 'true'.

KurtJanssen avatar Aug 10 '12 07:08 KurtJanssen

I have the same need. And since extenders do not work on computed observables, there does not seem to be an alternative.

enzoaeneas avatar Oct 30 '12 12:10 enzoaeneas

Carl Schroedl did some work on this, take a look here: https://github.com/carlschroedl/validatedviewmodel/wiki

jbeall avatar Nov 08 '12 21:11 jbeall

@KurtJanssen ,

As @jbeall mentioned, the ValidatedViewModel would allow you to dynamically enable/disable rules, as well as some other functionality that helps you re-use your view models.

@enzoaeneas, Sorry, I'm not sure what you mean by 'extenders'. Could you clarify that a bit? I know that you can validate computed observables (see examples in the ValidatedViewModel unit tests), but maybe I misunderstand... is that what you were getting at?

carlschroedl avatar Nov 13 '12 01:11 carlschroedl

@CarlSchroedl,

Yes. Since computed observable cannot be extended, which is how knockout validation works, I have run into the problem where I cannot turn validation on and off based on visibility of an element (which is in turn based on the value of another element or observable). I attempted to make the value validation based on a computed itself but it fails.

enzoaeneas avatar Nov 13 '12 01:11 enzoaeneas

Covered by onlyIf option available for rules.

crissdev avatar Nov 17 '14 19:11 crissdev