Knockout-Contrib / Knockout-Validation

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

Does Knockout-Validation support knockout-es5 #550

Open grofit opened 9 years ago

grofit commented 9 years ago

Hey,

I was just wondering if support had been added for using properties opposed to knockout observables as used in the knockout-es5 library. I cannot find much information on them, but as the .extend() is usually expecting an observable I was not sure if it would correctly work with the properties syntax used.

crissdev commented 9 years ago

I don't really use knockout-es5 but support for that might be added in the future. See #477

crissdev commented 9 years ago

Not really sure the following example is the way to go but seems to work just fine. https://jsfiddle.net/gf47bvyj/

grofit commented 9 years ago

Sorry missed this response. To my knowledge the purpose of knockout-es5 is to not use the foo = ko.observable('value') notion but instead just apply it via foo = 'value'; then use the ko.track(someInstance); as shown in the above example. The problem is the example above still uses the ko.observable which I believe is required for the .extend({}) call, so that was the focus of the question.

As there seems to be no way around the .extend invocation when there is no observable to apply it to, I tried it just to make sure in a forked version: https://jsfiddle.net/293k5759/ but it doesnt seem to react.

crissdev commented 9 years ago

@grofit Yes, you are right. I've updated the jsFiddle. Now it's pretty clear that's too much repetitive code to write - using ko.validation.setRules is not possible either. I'll do more research on this - maybe there's a solution for this.


Updated example https://jsfiddle.net/429xyj90/

marinasundstrom commented 9 years ago

What about parsing HTML5 attributes that are being data-bound to ES5-properties? I've tried it but knockout-validaition sends the value rather than the observable.

Is there away to make knockout-validation resolve these observables? How hard is it to implement?

grofit commented 8 years ago

Just wondering if there has been any progress on this?

grofit commented 8 years ago

The linked issue shows how to setup validation rules, however the group and other helpers for wrapping a vm and listening for errors seems to fail. I know that there was a discussion in:

https://github.com/SteveSanderson/knockout-es5/issues/2

Around how to handle this sort of stuff for other knockout libraries.

jmvtrinidad commented 7 years ago

I was able to work with ko.validation.group using knockout-es5 by using this function.

function validate() {
    var self = this;
    const allObservable = ko.es5.getAllObservablesForObject(self);
    const tempObj = {};
    for (let key in allObservable) {
        if (allObservable.hasOwnProperty(key)) {
            tempObj[key] = allObservable[key]();
        }
    }
    self.errors = ko.validation.group(tempObj);
    self.isValid = ko.pureComputed(function () {
        var valid = self.errors().length === 0;
        if (!valid)
            self.errors.showAllMessages();
        return valid;
    }, self);
}

And call it like this one after

`ko.track(this/* or self or your obj*/)`
validate.call(this/*your object here*/);