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

Do not add properties 'errors', 'isValid' and 'isAnyMessageShown' to grouped obj #225

Closed
wants to merge 3 commits into from

Conversation

delixfe
Copy link
Contributor

@delixfe delixfe commented Feb 8, 2013

The group function surprised me a bit by adding the following properties to view model:

var vm = {};
var group = ko.validation.group(vm);
typeof vm.errors === 'function'; // true
typeof vm.isValid=== 'function'; // true
typeof vm.isAnyMessageShown=== 'function'; // true

I stumbled over that after trying to find out why some unit tests failed (the reason being these properties).

Therefore I have added a new option addResultToVM to group which configures if to set these properties.

I have set that configuration to false because I think we should follow the principle of least surprise. But that might break existing code.

delixfe added 3 commits November 23, 2012 11:36
…s, isValid, isAnyMessageShown to the view model.

isValid and isAnyMessageShown are now also added to the result of group.
The reason is that these properties can cause problems if the view model already uses properties named like that.
Besides that it creates problems in combination with errorDetails.
@khakulov
Copy link
Contributor

@crissdev What do you think about this pull request? Can you merge changes to master?

@crissdev
Copy link
Member

Adding new configuration options is something I'm trying to avoid in order to keep the API simple. This means configuration options should be well justified before adding them.
The validatedObservable has been updated to allow grouping options to be passed in fd8f408 - so further extension to ko.validation.group is possible.
I can see how the properties published to the view model can cause some issues, but why would you use validatedObservable if you're not interested about the properties it adds?

@khakulov
Copy link
Contributor

ko.validation.init();

var form = ko.validatedObservable({
    first_name: ko.observable('John').extend({required: true}),
    last_name: ko.observable('Doe').extend({required: true}),
    email: ko.observable('john.doe(at)example.com').extend({required: true, email: true}),
});

form.isValid(); // <-- OK
form().isValid(); // <-- WTF???
form.errors(); // <-- OK
form().errors(); // <-- WTF???

form.errors.showAllMessages(); // <-- OK
form().isAnyMessageShown(); // <-- WTF??? should be form.errors.isAnyMessageShown()

ko.toJS(form()); // <-- WTF??? contains errors because email is not valid

@khakulov
Copy link
Contributor

@crissdev i did new pull request on actual master state #465

@crissdev
Copy link
Member

@khakulov Thanks. I'll take a closer look and see how mergeable it is 👍

@crissdev
Copy link
Member

Closed in favor of #465

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

Successfully merging this pull request may close these issues.

None yet

3 participants