Skip to content

Commit

Permalink
Fixed issues and update to 2.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
egucciar committed Oct 13, 2015
1 parent 22587c1 commit 12dba8a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-validation",
"version": "2.0.4",
"version": "2.0.5",
"description": "A KnockoutJS Plugin for model and property validation",
"main": "dist/knockout.validation.js",
"license": "MIT",
Expand Down
34 changes: 25 additions & 9 deletions dist/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ kv.configuration = configuration;
// 1. Just the params to be passed to the validator
// 2. An object containing the Message to be used and the Params to pass to the validator
// 3. A condition when the validation rule to be applied
// 4. An object containing the severity to be ysed
// 4. An object containing the severity to be used
//
// Example:
// var test = ko.observable(3).extend({
Expand Down Expand Up @@ -1207,12 +1207,13 @@ ko.extenders['validatable'] = function (observable, options) {
throttleEvaluation : options.throttle || config.throttle
};

observable.error = ko.observable(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 and has and has a severity of 1
observable.severity = ko.observable(1);

// observable.rules:
// ObservableArray of Rule Contexts, where a Rule Context is simply the name of a rule and the params to supply to it
//
// Rule Context = { rule: '<rule name>', params: '<passed in params>', message: '<Override of default Message>' }
// Rule Context = { rule: '<rule name>', params: '<passed in params>', message: '<Override of default Message>', severity: '<severity level' }
observable.rules = ko.observableArray(); //holds the rule Contexts to use as part of validation

//in case async validation is occurring
Expand All @@ -1227,11 +1228,12 @@ ko.extenders['validatable'] = function (observable, options) {
observable.isValid = ko.computed(observable.__valid__);

//manually set error state
observable.setError = function (error) {
observable.setError = function (error, severity) {
var previousError = observable.error.peek();
var previousIsValid = observable.__valid__.peek();

observable.error(error);
observable.severity(severity);
observable.__valid__(false);

if (previousError !== error && !previousIsValid) {
Expand Down Expand Up @@ -1299,8 +1301,8 @@ function validateSync(observable, rule, ctx) {
observable.setError(kv.formatMessage(
ctx.message || rule.message,
unwrap(ctx.params),
observable));
return false;
observable), ctx.severity);
return ctx.severity === 1 ? false : "warning";
} else {
return true;
}
Expand Down Expand Up @@ -1353,7 +1355,9 @@ kv.validateObservable = function (observable) {
rule, // the rule validator to execute
ctx, // the current Rule Context for the loop
ruleContexts = observable.rules(), //cache for iterator
len = ruleContexts.length; //cache for iterator
len = ruleContexts.length, //cache for iterator
result, // holds result of validate call
hasWarning; // holds result if there is a warning

for (; i < len; i++) {

Expand All @@ -1374,11 +1378,23 @@ kv.validateObservable = function (observable) {

} else {
//run normal sync validation
if (!validateSync(observable, rule, ctx)) {
result = validateSync(observable, rule, ctx);

if (result === 'warning') {
hasWarning = true;
}

if (!result) {
return false; //break out of the loop
}
}
}
if(hasWarning) {
// durring the loop we encountered a warning
// but wanted to keep looping incase there was an error
// so return false as if we had an error
return false;
}
//finally if we got this far, make the observable valid again!
observable.clearError();
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
// 1. Just the params to be passed to the validator
// 2. An object containing the Message to be used and the Params to pass to the validator
// 3. A condition when the validation rule to be applied
// 4. An object containing the severity to be ysed
// 4. An object containing the severity to be used
//
// Example:
// var test = ko.observable(3).extend({
Expand Down
30 changes: 22 additions & 8 deletions src/extenders.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ function validateSync(observable, rule, ctx) {
if (!rule.validator(observable(), (ctx.params === undefined ? true : ko.utils.unwrapObservable(ctx.params)))) { // default param is true, eg. required = true

//not valid, so format the error message and stick it in the 'error' variable
observable.setError(ko.validation.formatMessage(
observable.setError(kv.formatMessage(
ctx.message || rule.message,
ko.utils.unwrapObservable(ctx.params),
observable));
return false;
unwrap(ctx.params),
observable), ctx.severity);
return ctx.severity === 1 ? false : "warning";
} else {
return true;
}
Expand Down Expand Up @@ -185,12 +185,14 @@ function validateAsync(observable, rule, ctx) {
});
}

ko.validation.validateObservable = function (observable) {
kv.validateObservable = function (observable) {
var i = 0,
rule, // the rule validator to execute
ctx, // the current Rule Context for the loop
ruleContexts = observable.rules(), //cache for iterator
len = ruleContexts.length; //cache for iterator
len = ruleContexts.length, //cache for iterator
result, // holds result of validate call
hasWarning; // holds result if there is a warning

for (; i < len; i++) {

Expand All @@ -203,19 +205,31 @@ ko.validation.validateObservable = function (observable) {
}

//get the core Rule to use for validation
rule = ctx.rule ? ko.validation.rules[ctx.rule] : ctx;
rule = ctx.rule ? kv.rules[ctx.rule] : ctx;

if (rule['async'] || ctx['async']) {
//run async validation
validateAsync(observable, rule, ctx);

} else {
//run normal sync validation
if (!validateSync(observable, rule, ctx)) {
result = validateSync(observable, rule, ctx);

if (result === 'warning') {
hasWarning = true;
}

if (!result) {
return false; //break out of the loop
}
}
}
if(hasWarning) {
// durring the loop we encountered a warning
// but wanted to keep looping incase there was an error
// so return false as if we had an error
return false;
}
//finally if we got this far, make the observable valid again!
observable.clearError();
return true;
Expand Down

0 comments on commit 12dba8a

Please sign in to comment.