Knockout-Contrib / Knockout-Validation

A validation library for Knockout JS
1.02k stars 379 forks source link

Changing validation rules on the fly #49

Open altunyurt opened 12 years ago

altunyurt commented 12 years ago

I'm trying to update the validation rules of some fields that depend on the values of others on the fly, with no success. Sample code is at http://jsfiddle.net/2vfaV/3/ "deneme" is the main field and validation of "bagimli" depends on the value of deneme. How can i update the validation rule of bagimli?

ericmbarnard commented 12 years ago

@altunyurt

I believe this is happening because ko.validation is not over-writing your previously added rule, and instead is adding an additional 'required' rule. Also, the required rule hasn't really been tested with a false setting, as ko.validation is not really designed for changing rules on the fly.

For now you can probably workaround the issue by doing something like this: http://jsfiddle.net/2vfaV/7/

But, like I said... required isn't really expecting to have a 'false' value.

One other option you have is to do .extend({ validatable: false }); ... that will erase all validation from the observable, and then you can - re-assign validation to it. The only problem here is that you would have to call ko.applyBindings again.

altunyurt commented 12 years ago

@ericmbarnard thanks for the example. I've tried the validatable after posting the question with no success, yet i wasn't aware of rebinding.

carlschroedl commented 11 years ago

@altunyurt ,

I've applied some functionality from ValidatedViewModel to your example:

http://jsfiddle.net/carlschroedl/d9Mzv/

Does this behave how you expect?