From 23c33f70902a3b0da03dfe89edcb70a07faa70c5 Mon Sep 17 00:00:00 2001 From: delixfe Date: Thu, 6 Dec 2012 14:15:31 +0100 Subject: [PATCH 1/2] Added an extender 'ignoreInValidationGroup' This extender can be used to flag a Knockout Observable to be ignored in validation groups. When the extender is set the observable will not be added to validation groups. Rules added to a flagged obserable still apply but the errors are not shown in any group. --- Src/knockout.validation.js | 12 ++++++++++++ Tests/validation-tests.js | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Src/knockout.validation.js b/Src/knockout.validation.js index ab1422c3..0fcf3580 100644 --- a/Src/knockout.validation.js +++ b/Src/knockout.validation.js @@ -215,6 +215,11 @@ // if object is observable then add it to the list if (ko.isObservable(obj)) { + //but not if it is flagged as ignore in group + if(obj.ignoreInValidationGroup) { + return; // we also do not add descendants + } + //make sure it is validatable object if (!obj.isValid) obj.extend({ validatable: true }); validatables.push(obj); @@ -939,6 +944,13 @@ return observable; }; + //This extender can be used to flag a Knockout Observable to be ignored in validation groups. + //When the extender is set the observable will not be added to validation groups. + //Rules added to a flagged obserable still apply but the errors are not shown in any group. + ko.extenders['ignoreInValidationGroup'] = function (observable, enable) { + observable.ignoreInValidationGroup = enable; + }; + function validateSync(observable, rule, ctx) { //Execute the validator and see if its valid if (!rule.validator(observable(), ctx.params === undefined ? true : ctx.params)) { // default param is true, eg. required = true diff --git a/Tests/validation-tests.js b/Tests/validation-tests.js index 65896b6f..78970917 100644 --- a/Tests/validation-tests.js +++ b/Tests/validation-tests.js @@ -1038,6 +1038,18 @@ test('Issue #37 - Toggle ShowAllMessages', function () { ok(!vm.two.one.isModified(), "Level 2 is not modified"); ok(!vm.three.two.one.isModified(), "Level 3 is not modified"); }); + +test('grouping ignores observables extended with ignoreInValidationGroup', function () { + var vm = {}; + vm.firstName = ko.observable().extend({ required: true }); + vm.lastName = ko.observable().extend({ required: 2 }); + vm.allProperties = ko.observableArray([vm.firstName, vm.lastName]).extend({ ignoreInValidationGroup: true }); + + var errors = ko.validation.group(vm, { deep: true, observable: true }); + + equals(errors().length, 2, 'Grouping correctly finds only 2 invalid properties'); +}); + //#endregion //#region Conditional Validation From 9a991b1aeeb74dc2c76d6853d3664ef543183624 Mon Sep 17 00:00:00 2001 From: delixfe Date: Thu, 6 Dec 2012 14:27:43 +0100 Subject: [PATCH 2/2] Extender ignoreInValidationGroup return observable to enable chaining. --- Src/knockout.validation.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Src/knockout.validation.js b/Src/knockout.validation.js index 0fcf3580..ca34cfb6 100644 --- a/Src/knockout.validation.js +++ b/Src/knockout.validation.js @@ -949,6 +949,7 @@ //Rules added to a flagged obserable still apply but the errors are not shown in any group. ko.extenders['ignoreInValidationGroup'] = function (observable, enable) { observable.ignoreInValidationGroup = enable; + return observable; }; function validateSync(observable, rule, ctx) {