Skip to content

Commit

Permalink
improve valid result, you can set success or error message when valid…
Browse files Browse the repository at this point in the history
… the data
  • Loading branch information
liangkuaisheng committed Aug 23, 2016
1 parent c5e2603 commit 13068f1
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/validator.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,32 @@
*/
var focusElements = {};

/**
* Get Validation Result Object
* @param data
* @returns {
* result: Boolean, // is success or error
* message: String // tips
* }
*/
function getResultObj(data) {
var result = null;
if (data && data.length > 0) {
result = data[0];
if (!angular.isObject(result)) {
return {
result: result,
message: ''
};
}
} else {
result = {
result: false,
message: ''
}
}
return result;
}

/**
* Check Validation with Function or RegExp
Expand All @@ -150,16 +176,16 @@
var expression = $validationProvider.getExpression(validator);
var validationGroup = attrs.validationGroup;
var valid = {
success: function() {
validFunc(element, attrs[successMessage], validator, scope, ctrl, attrs);
success: function(message) {
validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs);
if (leftValidation.length) {
return checkValidation(scope, element, attrs, ctrl, leftValidation, value);
} else {
return true;
}
},
error: function() {
return invalidFunc(element, attrs[errorMessage], validator, scope, ctrl, attrs);
error: function(message) {
return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs);
}
};

Expand All @@ -172,12 +198,14 @@
if (expression.constructor === Function) {
return $q.all([$validationProvider.getExpression(validator)(value, scope, element, attrs, validatorParam)])
.then(function(data) {
if (data && data.length > 0 && data[0]) {
var resultObj = getResultObj(data);
var message = resultObj.message;
if (resultObj.result) {
if (validationGroup) {
groups[validationGroup][ctrl.$name] = true;
setValidationGroup(scope, validationGroup, true);
}
return valid.success();
return valid.success(message);
} else if (validationGroup) {
groups[validationGroup][ctrl.$name] = false;

Expand All @@ -187,9 +215,9 @@
setValidationGroup(scope, validationGroup, true);
} else {
setValidationGroup(scope, validationGroup, false);
return valid.error();
return valid.error(message);
}
} else return valid.error();
} else return valid.error(message);
}, function() {
return valid.error();
});
Expand Down

0 comments on commit 13068f1

Please sign in to comment.