diff --git a/Src/knockout.validation.js b/Src/knockout.validation.js index ab1422c3..ca34cfb6 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,14 @@ 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; + return observable; + }; + 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