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

Does Knockout-Validation support knockout-es5 #550

Open
grofit opened this issue Mar 4, 2015 · 8 comments
Open

Does Knockout-Validation support knockout-es5 #550

grofit opened this issue Mar 4, 2015 · 8 comments
Labels

Comments

@grofit
Copy link
Contributor

grofit commented Mar 4, 2015

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
Copy link
Member

crissdev commented Mar 4, 2015

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

@crissdev
Copy link
Member

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

@grofit
Copy link
Contributor Author

grofit commented Mar 17, 2015

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
Copy link
Member

@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
Copy link

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
Copy link
Contributor Author

grofit commented Aug 8, 2015

Just wondering if there has been any progress on this?

@grofit
Copy link
Contributor Author

grofit commented Aug 9, 2015

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:

SteveSanderson/knockout-es5#2

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

@jmvtrinidad
Copy link

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*/);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants