Knockout-Contrib / Knockout-Validation

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

Opt-out option for makeBindingHandlerValidatable #477

Open jods4 opened 9 years ago

jods4 commented 9 years ago

I would love if there was an option to opt-out of the 'hacking' of regular bindings with validation binding handlers. All it takes is put lines 46-51 of bindingHandlers.js inside a if.

My use case is that I use this validation library with property getters/setters (i.e. ko-es5) and because of this makeBindingHandlerValidatable does not work for me anyway (because it does not receive the observable object, only its value).

BTW, while I was reading this piece of code I noticed that ko.bindingHandlers.validationCore is executed in many bindings but is not the most efficient.

Why not wrap all the code inside a if (ko.validation.utils.isValidatable(observable))? If it's not true this function is a no-op. But currently it would still read the config for the element, check 3 different config settings and check the above condition 3 times :( As a side-effect it would also reduce code size.

crissdev commented 9 years ago

I see more control over default options, extenders registration is required and is something which will be addressed in the future.

crissdev commented 9 years ago

@jods4 Can you provide a fiddle as a starting point?

jods4 commented 9 years ago

Those are simple changes. When I have a bit of time I'll try to create a PR.

crissdev commented 9 years ago

I was actually referring to a fiddle using ko-es5 and knockout-validation. Creating a PR at this stage it's not recommended because this is something which might be/is related to initialization. Once there will be an issue created (by me, similar to #542) for this, a PR will be very welcome. Thanks anyway.

jods4 commented 9 years ago

Using ko-es5 together with knockout-validation is worthy more than a quick fiddle I'm afraid. There are several parts here:

1st setting up the validation rules. Could be done by creating the ko.observable() before calling ko.track() or using ko-es5 ko.getObservable() after the fact. We created a helper method than can take an object describing all the rules for a complete object and applies them at once to a "tracked" ko-es5 entity.

2nd creating the isValid observable at top object level. Pretty much done the same way as (1) with a helper utility method to the rescue.

3rd setting up the validation bindings. To avoid setting them up manually we created a KO binding pre-processor. It turns value: name into the moral equivalent of value: name; validationElement: ko.getObservable('name').

There's more to it but I guess you get the key ideas.