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

Updating a validatedObservable will not reset initial validation result #209

Closed
thomaux opened this issue Jan 10, 2013 · 6 comments · Fixed by #495
Closed

Updating a validatedObservable will not reset initial validation result #209

thomaux opened this issue Jan 10, 2013 · 6 comments · Fixed by #495
Labels

Comments

@thomaux
Copy link

thomaux commented Jan 10, 2013

I've encountered a problem regarding the validatedObservable.

When first set, validation occurs correctly, however after being set to a new observable, the validation result stays the same.

You can use this fiddle to reproduce the issue: http://jsfiddle.net/anzeo/56nLM/

Steps:

  1. Fill in a value and click 'add contact'
  2. Click 'update contact'
  3. Click 'add contact' it will alert true although no value is specified

(note: the scenario could be inversed by directly clicking on 'update contact' and then filling in a value and clicking 'add contact'

@mcottingham
Copy link

Is there a known workaround for this issue?

@DenEddie
Copy link

DenEddie commented Oct 9, 2013

Hi,

I think this project is a bit lost in what is really important. But whatever, I fixed this issue, but I'm not a contributor.

In the knockout.validation.js file just change:

ko.validatedObservable = function (initialValue) {
    if (!ko.validation.utils.isObject(initialValue)) { return ko.observable(initialValue).extend({ validatable: true }); }

    var obsv = ko.observable(initialValue);
    obsv.errors = ko.validation.group(initialValue);
    obsv.isValid = ko.computed(function () {
        return obsv.errors().length === 0;
    });

    return obsv;
};

into:

ko.validatedObservable = function (initialValue) {
    if (!ko.validation.utils.isObject(initialValue)) { return ko.observable(initialValue).extend({ validatable: true }); }

    var obsv = ko.observable();

    obsv.subscribe(function(newValue){
        obsv.errors = ko.validation.group(newValue);
        obsv.isValid = ko.computed(function () {
            return obsv.errors().length === 0;
        });
    });

    obsv(initialValue);
    return obsv;
};

Hopefully it will be added in the newest release.

@stevegreatrex
Copy link
Contributor

Hi @DenEddie, thanks for the contribution. There's no need to be a "contributor" though - you can fork the repo, make the change in your local branch and then raise a pull request.

Ideally we would want some unit tests as well though

@DenEddie
Copy link

Hi @stevegreatrex, I would love to contribute this partials code, but the problem is I didn't figure out how the current structure in code is done. I still use the 1 file library, but I see that they are being split up in multiple files. Let me know where you most like to see this and what sort of unit test you like and I contribute this :)

@stalniy
Copy link

stalniy commented Oct 27, 2013

Don't think that it's a good idea to hold a whole object inside observable. A better way is to create one view model inside another and then only updates its values using something like ko.viewmodel plugin or manually and reset modification state

@crissdev
Copy link
Member

obsv.subscribe(function(newValue) {
    obsv.errors = ko.validation.group(newValue);
    ...
});

This will not work because it overwrites the errors property causing all previous subscribers to not receive further notifications.

crissdev added a commit that referenced this issue Jan 7, 2015
This commit also adds an internal method to update the state for the validation group.
This method is not intended to be used directly but to provide a way to update the
group until a better API will be implemented.

Closes #442
Closes #269
Closes #209
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants