Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Commit

Permalink
temp commit to own repository
Browse files Browse the repository at this point in the history
Easter holiday, cant keep this code locked up in the office for a
week...
  • Loading branch information
tlastad committed Mar 31, 2015
1 parent a0628fa commit 32cc8a0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/validation-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ angular
//----
// Public functions declaration
//----------------------------------


function isValidationRequired(validations) {
return validations.indexOf("required") >= 0 || validations.indexOf("requiredselection") >= 0;
}
function defineValidation() {
var self = this;
var customUserRegEx = {};
Expand Down Expand Up @@ -91,8 +94,7 @@ angular
var validations = rules.split('|');

if(validations) {
self.bFieldRequired = (validations.indexOf("required") >= 0) ? true : false;

self.bFieldRequired = isValidationRequired(validations) ? true : false;
// loop through all validators of the element
for(var i = 0, ln = validations.length; i < ln; i++) {
var params = validations[i].split(':'); // params[0] is the rule, [1] is the rule extra params
Expand Down Expand Up @@ -125,7 +127,8 @@ angular
* @param string message: error message to display
*/
function updateErrorMsg(message, attrs) {
var self = this;
// attrs.obj if set should be a commonObj
var self = (!!attrs && attrs.obj) ? attrs.obj : this;

// element name could be defined in the `attrs` or in the self object
var elm = (!!attrs && attrs.elm) ? attrs.elm : self.elm;
Expand Down Expand Up @@ -297,7 +300,7 @@ angular
if(index >= 0 && message === '') {
validationSummary.splice(index, 1);
}else if(message !== '') {
var errorObj = { field: elmName, message: message };
var errorObj = { field: elmName, message: message, obj: self};

// if error already exist then refresh the error object inside the array, else push it to the array
if(index >= 0) {
Expand Down
7 changes: 7 additions & 0 deletions src/validation-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,13 @@ angular
type: "regex"
};
break;
case "requiredselection":
validator = {
pattern: "\\S+",
message: "INVALID_REQUIREDSELECTION",
type: "regex"
};
break;
} // switch()

return validator;
Expand Down
20 changes: 13 additions & 7 deletions src/validation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ angular
validationService.prototype.addValidator = addValidator;
validationService.prototype.checkFormValidity = checkFormValidity;
validationService.prototype.removeValidator = removeValidator;
validationService.prototype.clearValidationSummary = clearValidationSummary;
validationService.prototype.setGlobalOptions = setGlobalOptions;

return validationService;
Expand Down Expand Up @@ -73,7 +74,7 @@ angular
attrs = mergeObjects(self.validationAttrs, attrs);

// watch the element for any value change, validate it once that happen
attrs.scope.$watch(attrs.elmName, function (newVal, oldVal) {
attrs.scope.$watch(attrs.elmName, function (newVal, oldVal) {
if(newVal === undefined && oldVal !== undefined) {
self.commonObj.updateErrorMsg("INVALID_KEY_CHAR", {valid: false, translate: true});
return;
Expand All @@ -84,11 +85,17 @@ angular

self.commonObj.initialize(attrs.scope, attrs.elm, attrs, attrs.ctrl);
attemptToValidate(self, newVal);
}, true); // $watch()
}, true); // $watch()

return self;
} // addValidator()
} // addValidator()

function clearValidationSummary(obj) {
if (typeof obj === "undefined" || typeof obj.$validationSummary === "undefined") {
throw 'checkFormValidity() requires a valid Angular Form or $scope object passed as argument to function properly (ex.: $scope.form1 OR $scope).';
}
obj.$validationSummary = [];
}
/** Is the Form all valid? Loop through Validation Summary to get the answer, if any errors are there then display them and return false
* @param object Angular Form or Scope Object
* @return bool isFormValid
Expand All @@ -103,13 +110,12 @@ angular
// loop through $validationSummary and display errors when found on each field
for(var i = 0, ln = obj.$validationSummary.length; i < ln; i++) {
isValid = false;
elmName = obj.$validationSummary[i].field;
elm = angular.element(document.querySelector('[name="'+elmName+'"]:not([disabled]):not([ng-disabled]'));
ctrl = angular.element(elm).controller('ngModel');
elm = obj.$validationSummary[i].obj.elm;
ctrl = obj.$validationSummary[i].obj.ctrl;

if(!!elm && elm.length > 0) {
ctrl.$setTouched(); // make the element as it was touched for CSS
self.commonObj.updateErrorMsg(obj.$validationSummary[i].message, {valid: false, elm: elm, submitted: true});
self.commonObj.updateErrorMsg(obj.$validationSummary[i].message, { valid: false, obj: obj.$validationSummary[i].obj, submitted: true });
}
}
return isValid;
Expand Down

0 comments on commit 32cc8a0

Please sign in to comment.