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

validatedObservable group options #461

Closed
danjohnso opened this issue Nov 4, 2014 · 6 comments
Closed

validatedObservable group options #461

danjohnso opened this issue Nov 4, 2014 · 6 comments

Comments

@danjohnso
Copy link

I don't think anyone is keeping this up anymore, but I found this change helpful when working with nested view models. Without being able to pass the deep option on instantiation, the validation will not give you all the errors or hide all the errors, so I added in the options to the constructor and passed them to the initial group.

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

    var obsv = ko.observable(initialValue);
    obsv.errors = kv.group(initialValue, options);
    obsv.isValid = ko.observable(initialValue.isValid());
    obsv.errors.subscribe(function (errors) {
        obsv.isValid(errors.length === 0);
    });

    return obsv;
};
@crissdev
Copy link
Member

crissdev commented Nov 5, 2014

@danda1man I belive using a validation group you can achieve this. See the wiki for an example.

@danjohnso
Copy link
Author

You can bit since a group is created on the original model, there are a few exceptions that will occur if you have other objects in your view model. My specific case was i was trying to hide error messages after mapping existing data into my model, but nested objects errors were still showing. If i made those nested objects validatedObservables, errors are thrown when calling: viewModel.errors.showAllMessages (false)

This was a simple fix with less overhead than creating extra groups

@crissdev
Copy link
Member

crissdev commented Nov 6, 2014

@danda1man Please provide a fiddle/plunk or some tests for this. Thanks.
A starting point for a plunk is available here

@danjohnso
Copy link
Author

Broken as is: http://plnkr.co/7R97MF7fGVfU1nL1csle

Anything deeper than the root level will not get caught in default grouping and so the errors will show. The fix as I mentioned earlier is modifying the validatedObservable constructor to take options so you can pass deep to the grouping. Here is a plunkr of my modification: http://plnkr.co/109BanVzSJ5iCi4Vwer0

@crissdev
Copy link
Member

crissdev commented Nov 8, 2014

@danda1man Thanks. This looks good. I'll write some tests for this and submit a pull request, unless you want to do it.

@danjohnso
Copy link
Author

Go ahead :)

@crissdev crissdev mentioned this issue Nov 10, 2014
crissdev added a commit that referenced this issue Nov 10, 2014
- Allow grouping options to be provided in order to make validatedObservable work with nested view models (#461)
- Prevent validatedObservable failure when grouping.observable is false (#454)
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