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

Fixes issue #247 #248

Merged
merged 1 commit into from
Apr 5, 2013
Merged
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
8 changes: 4 additions & 4 deletions Src/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@
ko.extenders['validatable'] = function (observable, enable) {
if (enable && !utils.isValidatable(observable)) {

observable.error = null; // holds the error message, we only need one since we stop processing validators when one is invalid
observable.error = ko.observable(null); // holds the error message, we only need one since we stop processing validators when one is invalid

// observable.rules:
// ObservableArray of Rule Contexts, where a Rule Context is simply the name of a rule and the params to supply to it
Expand Down Expand Up @@ -944,7 +944,7 @@
if (!rule.validator(observable(), ctx.params === undefined ? true : ctx.params)) { // default param is true, eg. required = true

//not valid, so format the error message and stick it in the 'error' variable
observable.error = exports.formatMessage(ctx.message || rule.message, ctx.params);
observable.error(exports.formatMessage(ctx.message || rule.message, ctx.params));
observable.__valid__(false);
return false;
} else {
Expand Down Expand Up @@ -977,7 +977,7 @@

if (!isValid) {
//not valid, so format the error message and stick it in the 'error' variable
observable.error = exports.formatMessage(msg || ctx.message || rule.message, ctx.params);
observable.error(exports.formatMessage(msg || ctx.message || rule.message, ctx.params));
observable.__valid__(isValid);
}

Expand Down Expand Up @@ -1020,7 +1020,7 @@
}
}
//finally if we got this far, make the observable valid again!
observable.error = null;
observable.error(null);
observable.__valid__(true);
return true;
};
Expand Down
78 changes: 39 additions & 39 deletions Tests/validation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ test('Issue #33 - Arrays - Invalid', function () {
.extend({ minLength: 4 });

testObj(['one', 'two', 'three']);
ok(!testObj.isValid(), testObj.error);
ok(!testObj.isValid(), testObj.error());
});
//#endregion

Expand Down Expand Up @@ -228,7 +228,7 @@ test('Issue #33 - Arrays - Invalid', function () {
.extend({ maxLength: 2 });

testObj(['one', 'two', 'three']);
ok(!testObj.isValid(), testObj.error);
ok(!testObj.isValid(), testObj.error());
});
//#endregion

Expand Down Expand Up @@ -405,17 +405,17 @@ test('Object is NOT Valid and isValid returns False', function () {
testObj('text#example.com');

equal(testObj(), 'text#example.com', 'observable still works');
equal( testObj.isValid(), false, testObj.error );
equal( testObj.error, 'Please enter a proper email address', "Error Message Needs to be formatted correctly" );
equal( testObj.isValid(), false, testObj.error());
equal( testObj.error(), 'Please enter a proper email address', "Error Message Needs to be formatted correctly" );
});

test('Email with invalid domain', function(){
var testObj = ko.observable().extend({ email: true });

testObj("john@abc.com123");

equal( testObj.isValid(), false, testObj.error );
equal( testObj.error, 'Please enter a proper email address');
equal( testObj.isValid(), false, testObj.error());
equal( testObj.error(), 'Please enter a proper email address');
})
//#endregion

Expand Down Expand Up @@ -445,7 +445,7 @@ test('Object is NOT Valid and isValid returns False', function () {
testObj('stuff');

equal(testObj(), 'stuff', 'observable still works');
equal(testObj.isValid(), false, testObj.error);
equal(testObj.isValid(), false, testObj.error());
});

//#endregion
Expand Down Expand Up @@ -476,7 +476,7 @@ test('Object is NOT Valid and isValid returns False', function () {
testObj('stuff');

equal(testObj(), 'stuff', 'observable still works');
equal(testObj.isValid(), false, testObj.error);
equal(testObj.isValid(), false, testObj.error());
});

//#endregion
Expand Down Expand Up @@ -507,7 +507,7 @@ test('Object is NOT Valid and isValid returns False', function () {
testObj('stuff');

equal(testObj(), 'stuff', 'observable still works');
equal(testObj.isValid(), false, testObj.error);
equal(testObj.isValid(), false, testObj.error());
});

//#endregion
Expand Down Expand Up @@ -538,7 +538,7 @@ test('Object is NOT Valid and isValid returns False', function () {
testObj('stuff');

equal(testObj(), 'stuff', 'observable still works');
equal(testObj.isValid(), false, testObj.error);
equal(testObj.isValid(), false, testObj.error());
});

//#endregion
Expand Down Expand Up @@ -611,7 +611,7 @@ test('Custom Rule Is NOT Valid Test', function () {
testObj(6);

equal(testObj(), 6, 'observable still works');
ok(testObj.error, testObj.error);
ok(testObj.error(), testObj.error());
equal(testObj.isValid(), false, 'testObj is valid');
});

Expand All @@ -632,7 +632,7 @@ test('Custom Message Correctly appears', function () {

equal(testObj(), '', 'observable still works');
equal(testObj.isValid(), false, 'testObj is valid');
equal(testObj.error, 'This Message is Special', "Message appears correctly");
equal(testObj.error(), 'This Message is Special', "Message appears correctly");
});

//#endregion
Expand Down Expand Up @@ -672,7 +672,7 @@ test('Object is Valid and isValid returns True', function () {

equal(testObj(), 4, 'observable still works');
equal(testObj.isValid(), false, 'testObj is valid');
equal(testObj.error, 'Must Equal 5', 'Error Message Matches');
equal(testObj.error(), 'Must Equal 5', 'Error Message Matches');
});


Expand All @@ -698,7 +698,7 @@ test( 'Issue #81 - Dynamic messages', function () {
testObj( 4 );

equal( testObj.isValid(), false, 'testObj is not valid' );
equal( testObj.error, 'after', 'testObj changes messages dynamically' );
equal( testObj.error(), 'after', 'testObj changes messages dynamically' );
});

//#endregion
Expand All @@ -716,16 +716,16 @@ test('Object is Valid and isValid returns True', function () {
}
});

ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('required') > -1, "required is first error");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('required') > -1, "required is first error");

testObj('s');
ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('at least') > -1, "Minimum Length not met");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('at least') > -1, "Minimum Length not met");

testObj('som');
ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('must contain') > -1, "Doesn't match required pattern");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('must contain') > -1, "Doesn't match required pattern");

});

Expand All @@ -739,16 +739,16 @@ test('Object is Valid and isValid returns True', function () {
}
});

ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('required') > -1, "required is first error");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('required') > -1, "required is first error");

testObj('s');
ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('at least') > -1, "Minimum Length not met");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('at least') > -1, "Minimum Length not met");

testObj('som');
ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('must contain') > -1, "Doesn't match required pattern");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('must contain') > -1, "Doesn't match required pattern");

});

Expand All @@ -757,26 +757,26 @@ test("Issue #47 - Validation chaining issue with required and email rules", func
.extend({ required: true })
.extend({ email: { message: 'Invalid email address.' } });

ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('required') > -1, "required is first error");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('required') > -1, "required is first error");

testObj('s'); // an invalid email address
ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('Invalid email') > -1, "Email error is second error");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('Invalid email') > -1, "Email error is second error");
});

test("Issue #43 - Error messages are not switched correctly", function () {
var testObj = ko.observable().extend({ min: 1, max: 100 });

testObj(-1); // should invalidate the min rule

ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('enter a value greater than') > -1, "Min rule was correctly triggered");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('enter a value greater than') > -1, "Min rule was correctly triggered");

testObj(101); // should invalidate the max rule

ok(!testObj.isValid(), testObj.error);
ok(testObj.error.indexOf('enter a value less than') > -1, "Max rule was correctly triggered");
ok(!testObj.isValid(), testObj.error());
ok(testObj.error().indexOf('enter a value less than') > -1, "Max rule was correctly triggered");
});

test("Issue #43 - Grouping - Error messages are not switched correctly", function () {
Expand All @@ -789,13 +789,13 @@ test("Issue #43 - Grouping - Error messages are not switched correctly", functio

vm.testObj(-1); // should invalidate the min rule

ok(!vm.testObj.isValid(), vm.testObj.error);
ok(vm.testObj.error.indexOf('enter a value greater than') > -1, "Min rule was correctly triggered");
ok(!vm.testObj.isValid(), vm.testObj.error());
ok(vm.testObj.error().indexOf('enter a value greater than') > -1, "Min rule was correctly triggered");

vm.testObj(101); // should invalidate the max rule

ok(!vm.testObj.isValid(), vm.testObj.error);
ok(vm.testObj.error.indexOf('enter a value less than') > -1, "Max rule was correctly triggered");
ok(!vm.testObj.isValid(), vm.testObj.error());
ok(vm.testObj.error().indexOf('enter a value less than') > -1, "Max rule was correctly triggered");
});

test('Issue #78 - Falsy Params', function () {
Expand Down Expand Up @@ -1250,7 +1250,7 @@ asyncTest('Async Rule Is NOT Valid Test', function () {

var doAssertions = function () {
equal(testObj(), 4, 'observable still works');
ok(testObj.error, testObj.error);
ok(testObj.error(), testObj.error());
equal(testObj.isValid(), false, 'testObj is not valid');
};

Expand Down
8 changes: 4 additions & 4 deletions Tests/validation-ui-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ test("Issue #43 & #47 - Error messages are not switched correctly", function ()

vm.testObj(-1); // should invalidate the min rule

ok(!vm.testObj.isValid(), vm.testObj.error);
equal(vm.testObj.error, $msg.text(), "Min rule was correctly triggered");
ok(!vm.testObj.isValid(), vm.testObj.error());
equal(vm.testObj.error(), $msg.text(), "Min rule was correctly triggered");

vm.testObj(101); // should invalidate the max rule

ok(!vm.testObj.isValid(), vm.testObj.error);
equal(vm.testObj.error, $msg.text(), "Max rule was correctly triggered");
ok(!vm.testObj.isValid(), vm.testObj.error());
equal(vm.testObj.error(), $msg.text(), "Max rule was correctly triggered");
});

test("Issue #44 - Validation Element - Is Valid Test", function () {
Expand Down