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

Rules are added multiple times when options.parseInputAttributes and options.writeInputAttributes are both set to true. #277

Closed
benpriebe opened this issue May 23, 2013 · 3 comments
Assignees
Labels
Milestone

Comments

@benpriebe
Copy link

When the parseInputAttributes and writeInputAttributes options are set to true; the writeInputAttributes occurs first; then the parseInputAttributes comes along after and says hey, there's some input attributes that i should go create rules for - even though we already have the rule.

See fiddle for verification:
http://jsfiddle.net/t2y5F/

Suggestion:

Modify addRule() function to check the rule isn't there already. Something like:

 addRule: function(observable, rule) {
                if (!utils.isValidatable(observable))
                    observable.extend({ validatable: true });

                var hasRule;
                ko.utils.arrayForEach(observable.rules(), function(arrRule) {
                    if (arrRule.name == rule.name) {
                        hasRule = true;
                        return;
                    }
                });

                if (!hasRule) {
                    //push a Rule Context to the observables local array of Rule Contexts
                    observable.rules.push(rule);
                }
                return observable;
            },
@crissdev
Copy link
Member

Confirmed.
http://jsfiddle.net/1rhde7o0/

@crissdev crissdev added the bug label Dec 16, 2014
@crissdev crissdev added this to the v2.0 milestone Jan 7, 2015
@crissdev
Copy link
Member

crissdev commented Jan 7, 2015

The rule will not be added if it already exists. This will apply only to rules that have a name.

var obsv = ko.observable(1).extend({ min: 10 });

// obsv.rules().length is 1

obsv.extend({ min: 20 }); // The rule will NOT be added because it already exists

// obsv.rules().length is still 1

For the upcoming releases we might consider #441 - preferably avoiding breaking changes.

@crissdev crissdev self-assigned this Jan 7, 2015
crissdev added a commit that referenced this issue Jan 7, 2015
Fix #277 - parseInputAttributes option may duplicate rules when enabled
@Helen1987
Copy link

Hm, actually this change is breaking change for me. I need a possibility to set a rule with the same name at least twice in some cases (although rule name is the same, parameters are different). I even created a bug #618. Can be this issue fixed in a different way?

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

3 participants