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

Added clearAllErrors and validateAllObservables functions #433

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions Dist/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,19 +353,39 @@ kv.configuration = configuration;
};
}

result.showAllMessages = function (show) { // thanks @heliosPortal
if (show === undefined) {//default to true
show = true;
}

function iterateObservables(callback) {
// ensure we have latest changes
result();

forEach(context.validatables, function (observable) {
callback(observable);
});
}

result.showAllMessages = function (show) { // thanks @heliosPortal
if (show === undefined) {//default to true
show = true;
}

iterateObservables(function (observable) {
observable.isModified(show);
});
};

result.clearAllErrors = function () {
iterateObservables(function (observable) {
observable.clearError();
observable.isModified(false);
});
};

result.validateAllObservables = function () {
iterateObservables(function (observable) {
kv.validateObservable(observable);
observable.isModified(true);
});
};

obj.errors = result;
obj.isValid = function () {
return obj.errors().length === 0;
Expand Down
2 changes: 1 addition & 1 deletion Dist/knockout.validation.min.js

Large diffs are not rendered by default.

30 changes: 25 additions & 5 deletions Src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,39 @@
};
}

result.showAllMessages = function (show) { // thanks @heliosPortal
if (show === undefined) {//default to true
show = true;
}

function iterateObservables(callback) {
// ensure we have latest changes
result();

ko.utils.arrayForEach(context.validatables, function (observable) {
callback(observable);
});
}

result.showAllMessages = function (show) { // thanks @heliosPortal
if (show === undefined) {//default to true
show = true;
}

iterateObservables(function (observable) {
observable.isModified(show);
});
};

result.clearAllErrors = function () {
iterateObservables(function (observable) {
observable.clearError();
observable.isModified(false);
});
};

result.validateAllObservables = function () {
iterateObservables(function (observable) {
ko.validation.validateObservable(observable);
observable.isModified(true);
});
};

obj.errors = result;
obj.isValid = function () {
return obj.errors().length === 0;
Expand Down
225 changes: 225 additions & 0 deletions Tests/utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,231 @@
//#region Utils Tests

module("Utils tests");
test('Issue #431 - Recursively clearAllErrors', function () {
var vm = {
one: ko.observable().extend({ required: true }),
two: {
one: ko.observable().extend({ required: true })
},
three: {
two: {
one: ko.observable().extend({ required: true })
}
}
};

var validation = ko.validation.group(vm, { deep: true, observable: false });

// edit all the properties, but set them so the required rule fails
vm.one("test");
vm.one("");
vm.two.one("test");
vm.two.one("");
vm.three.two.one("test");
vm.three.two.one("");

ok(vm.one.isModified(), "Level 1 is modified");
ok(vm.two.one.isModified(), "Level 2 is modified");
ok(vm.three.two.one.isModified(), "Level 3 is modified");
equal(validation().length, 3, 'Grouping correctly finds 3 invalid properties');

// now clear all errors
validation.clearAllErrors();

ok(!vm.one.isModified(), "Level 1 is not modified");
ok(!vm.two.one.isModified(), "Level 2 is not modified");
ok(!vm.three.two.one.isModified(), "Level 3 is not modified");

equal(validation().length, 0, 'Grouping should find zero invalid properties');
});

test('Issue #431 - Recursively clearAllErrors - using computed', function () {
var vm = {
one: ko.observable().extend({ required: true }),
two: {
one: ko.observable().extend({ required: true })
},
three: {
two: {
one: ko.observable().extend({ required: true })
}
}
};

var validation = ko.validation.group(vm, { deep: true, observable: true });

// edit all the properties, but set them so the required rule fails
vm.one("test");
vm.one("");
vm.two.one("test");
vm.two.one("");
vm.three.two.one("test");
vm.three.two.one("");

ok(vm.one.isModified(), "Level 1 is modified");
ok(vm.two.one.isModified(), "Level 2 is modified");
ok(vm.three.two.one.isModified(), "Level 3 is modified");
equal(validation().length, 3, 'Grouping correctly finds 3 invalid properties');

// now clear all errors
validation.clearAllErrors();

ok(!vm.one.isModified(), "Level 1 is not modified");
ok(!vm.two.one.isModified(), "Level 2 is not modified");
ok(!vm.three.two.one.isModified(), "Level 3 is not modified");

equal(validation().length, 0, 'Grouping should find zero invalid properties');
});

test('Issue #431 - Recursively validateAllObservables', function () {
var vm = {
one: ko.observable().extend({ required: true }),
two: {
one: ko.observable().extend({ required: true })
},
three: {
two: {
one: ko.observable().extend({ required: true })
}
}
};

var validation = ko.validation.group(vm, { deep: true, observable: false });

// edit all the properties, but set them so the required rule fails
vm.one("test");
vm.one("");
vm.two.one("test");
vm.two.one("");
vm.three.two.one("test");
vm.three.two.one("");

equal(validation().length, 3, 'Grouping correctly finds 3 invalid properties');

// now clear all errors
validation.clearAllErrors();

equal(validation().length, 0, 'Grouping correctly finds 0 invalid properties');

// invoke all errors
validation.validateAllObservables();

ok(vm.one.isModified(), "Level 1 is modified");
ok(vm.two.one.isModified(), "Level 2 is modified");
ok(vm.three.two.one.isModified(), "Level 3 is modified");

equal(validation().length, 3, 'Grouping should find 3 invalid properties');
});

test('Issue #431 - Recursively validateAllObservables - using computed', function () {
var vm = {
one: ko.observable().extend({ required: true }),
two: {
one: ko.observable().extend({ required: true })
},
three: {
two: {
one: ko.observable().extend({ required: true })
}
}
};

var validation = ko.validation.group(vm, { deep: true, observable: true });

// edit all the properties, but set them so the required rule fails
vm.one("test");
vm.one("");
vm.two.one("test");
vm.two.one("");
vm.three.two.one("test");
vm.three.two.one("");

equal(validation().length, 3, 'Grouping correctly finds 3 invalid properties');

// now clear all errors
validation.clearAllErrors();

equal(validation().length, 0, 'Grouping correctly finds 0 invalid properties');

// invoke all errors
validation.validateAllObservables();

ok(vm.one.isModified(), "Level 1 is modified");
ok(vm.two.one.isModified(), "Level 2 is modified");
ok(vm.three.two.one.isModified(), "Level 3 is modified");

equal(validation().length, 3, 'Grouping should find 3 invalid properties');
});

test('Issue #431 - Recursively validateAllObservables, without calling clearAllErrors', function () {
var vm = {
one: ko.observable().extend({ required: true }),
two: {
one: ko.observable().extend({ required: true })
},
three: {
two: {
one: ko.observable().extend({ required: true })
}
}
};

var validation = ko.validation.group(vm, { deep: true, observable: false });

// edit all the properties, but set them so the required rule fails
vm.one("test");
vm.one("");
vm.two.one("test");
vm.two.one("");
vm.three.two.one("test");
vm.three.two.one("");

equal(validation().length, 3, 'Grouping correctly finds 3 invalid properties');

// invoke all errors
validation.validateAllObservables();

ok(vm.one.isModified(), "Level 1 is modified");
ok(vm.two.one.isModified(), "Level 2 is modified");
ok(vm.three.two.one.isModified(), "Level 3 is modified");

equal(validation().length, 3, 'Grouping should find 3 invalid properties');
});

test('Issue #431 - Recursively validateAllObservables, without calling clearAllErrors - using computed', function () {
var vm = {
one: ko.observable().extend({ required: true }),
two: {
one: ko.observable().extend({ required: true })
},
three: {
two: {
one: ko.observable().extend({ required: true })
}
}
};

var validation = ko.validation.group(vm, { deep: true, observable: true });

// edit all the properties, but set them so the required rule fails
vm.one("test");
vm.one("");
vm.two.one("test");
vm.two.one("");
vm.three.two.one("test");
vm.three.two.one("");

equal(validation().length, 3, 'Grouping correctly finds 3 invalid properties');

// invoke all errors
validation.validateAllObservables();

ok(vm.one.isModified(), "Level 1 is modified");
ok(vm.two.one.isModified(), "Level 2 is modified");
ok(vm.three.two.one.isModified(), "Level 3 is modified");

equal(validation().length, 3, 'Grouping should find 3 invalid properties');
});

test('Issue #31 - Recursively Show All Messages', function () {
var vm = {
Expand Down