Skip to content

Commit

Permalink
Merge pull request Knockout-Contrib#527 from Knockout-Contrib/issue-526
Browse files Browse the repository at this point in the history
Fix - Validation cannot be removed from attached observable
  • Loading branch information
crissdev committed Jan 28, 2015
2 parents 9c2f4d3 + 65ce414 commit a6931af
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
12 changes: 3 additions & 9 deletions dist/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1274,15 +1274,6 @@ ko.extenders['validatable'] = function (observable, options) {
//first dispose of the subscriptions
observable.isValid.dispose();
observable.rules.removeAll();
if (observable.isModified.getSubscriptionsCount() > 0) {
observable.isModified._subscriptions['change'] = [];
}
if (observable.isValidating.getSubscriptionsCount() > 0) {
observable.isValidating._subscriptions['change'] = [];
}
if (observable.__valid__.getSubscriptionsCount() > 0) {
observable.__valid__._subscriptions['change'] = [];
}
h_change.dispose();
h_obsValidationTrigger.dispose();

Expand All @@ -1292,6 +1283,9 @@ ko.extenders['validatable'] = function (observable, options) {
delete observable['isValidating'];
delete observable['__valid__'];
delete observable['isModified'];
delete observable['setError'];
delete observable['clearError'];
delete observable['_disposeValidation'];
};
} else if (options.enable === false && observable._disposeValidation) {
observable._disposeValidation();
Expand Down
2 changes: 1 addition & 1 deletion dist/knockout.validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/knockout.validation.min.js.map

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions src/extenders.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ ko.extenders['validatable'] = function (observable, options) {
//first dispose of the subscriptions
observable.isValid.dispose();
observable.rules.removeAll();
if (observable.isModified.getSubscriptionsCount() > 0) {
observable.isModified._subscriptions['change'] = [];
}
if (observable.isValidating.getSubscriptionsCount() > 0) {
observable.isValidating._subscriptions['change'] = [];
}
if (observable.__valid__.getSubscriptionsCount() > 0) {
observable.__valid__._subscriptions['change'] = [];
}
h_change.dispose();
h_obsValidationTrigger.dispose();

Expand All @@ -125,6 +116,9 @@ ko.extenders['validatable'] = function (observable, options) {
delete observable['isValidating'];
delete observable['__valid__'];
delete observable['isModified'];
delete observable['setError'];
delete observable['clearError'];
delete observable['_disposeValidation'];
};
} else if (options.enable === false && observable._disposeValidation) {
observable._disposeValidation();
Expand Down
9 changes: 8 additions & 1 deletion test/api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ QUnit.test('Basic Removal', function(assert) {
testObj(3);

var testFlag = false;
var changeFlag = false;

assert.equal(testObj(), 3, 'observable still works');
assert.ok(testObj.isValid(), 'testObj is Valid');
Expand All @@ -886,11 +887,16 @@ QUnit.test('Basic Removal', function(assert) {
testFlag = true;
});

testObj.isModified.subscribe(function() {
changeFlag = true;
});

testObj.extend({ validatable: false });

assert.ok(!testObj.isValid, 'Validation features removed');
testObj(1);
assert.ok(!testFlag, 'Subscriptions to isValid didnt fire');
assert.ok(!testFlag, 'Subscriptions to isValid did not fire');
assert.ok(!changeFlag, 'Subscriptions to isModified did not fire');
});

//#endregion
Expand Down Expand Up @@ -1082,3 +1088,4 @@ QUnit.test('can be invoked with (viewModel, rootNode, options)', function(assert
ko.validation.utils.setDomData = _setDomData;
});


14 changes: 14 additions & 0 deletions test/validation-ui-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ QUnit.test('Issue #277 - parseInputAttributes does not duplicate rules when pars
}, 1);
});

QUnit.test('Issue #526 - validation cannot be removed from attached observable', function(assert) {

var testObj = ko.observable(1).extend({ min: 2 });

addTestHtml('<input id="myTestInput" data-bind="value: value" type="text" />');
applyTestBindings({value: testObj});

assert.violatesMinRule(testObj, 1, 2);
assert.ok(ko.validation.utils.isValidatable(testObj));

testObj.extend({validatable: false});
assert.equal(ko.validation.utils.isValidatable(testObj), false);
});

//#region Inserting Messages

QUnit.test('Inserting Messages Works', function(assert) {
Expand Down

0 comments on commit a6931af

Please sign in to comment.