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

Initial value of IsValid of validatedObservable is not correct. #387

Closed
lucasmaj opened this issue Dec 27, 2013 · 3 comments
Closed

Initial value of IsValid of validatedObservable is not correct. #387

lucasmaj opened this issue Dec 27, 2013 · 3 comments

Comments

@lucasmaj
Copy link
Contributor

!!ko.validatedObservable({
   property1: ko.observable().extend({ required: false })
}).isValid()

You would expect true but isValid incorrectly reports falseish value undefined.

In other words, if your initial validated obvservable is correct, isValid will incorrectly keep reporting as NOT valid.

Proposed solution:

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

    var obsv = ko.observable(initialValue);
    obsv.errors = kv.group(initialValue);
    obsv.isValid = ko.observable(initialValue.isValid()); //pass initial value
    obsv.errors.subscribe(function (errors) {
        obsv.isValid(errors.length === 0);
    });

    return obsv;
};

Should I create a fork for it? Should I add a test case somewhere?

@stevegreatrex
Copy link
Contributor

Hi @lucasmaj, thanks for that. If you create a new PR with a fix & test case then I'll get it merged into the main branch

@lucasmaj
Copy link
Contributor Author

Done here: #393

@stevegreatrex
Copy link
Contributor

Fixed by #393

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

No branches or pull requests

2 participants