Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit 32cc8a0

Browse files
committed
temp commit to own repository
Easter holiday, cant keep this code locked up in the office for a week...
1 parent a0628fa commit 32cc8a0

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/validation-common.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ angular
5353
//----
5454
// Public functions declaration
5555
//----------------------------------
56-
56+
57+
function isValidationRequired(validations) {
58+
return validations.indexOf("required") >= 0 || validations.indexOf("requiredselection") >= 0;
59+
}
5760
function defineValidation() {
5861
var self = this;
5962
var customUserRegEx = {};
@@ -91,8 +94,7 @@ angular
9194
var validations = rules.split('|');
9295

9396
if(validations) {
94-
self.bFieldRequired = (validations.indexOf("required") >= 0) ? true : false;
95-
97+
self.bFieldRequired = isValidationRequired(validations) ? true : false;
9698
// loop through all validators of the element
9799
for(var i = 0, ln = validations.length; i < ln; i++) {
98100
var params = validations[i].split(':'); // params[0] is the rule, [1] is the rule extra params
@@ -125,7 +127,8 @@ angular
125127
* @param string message: error message to display
126128
*/
127129
function updateErrorMsg(message, attrs) {
128-
var self = this;
130+
// attrs.obj if set should be a commonObj
131+
var self = (!!attrs && attrs.obj) ? attrs.obj : this;
129132

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

302305
// if error already exist then refresh the error object inside the array, else push it to the array
303306
if(index >= 0) {

src/validation-rules.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@ angular
538538
type: "regex"
539539
};
540540
break;
541+
case "requiredselection":
542+
validator = {
543+
pattern: "\\S+",
544+
message: "INVALID_REQUIREDSELECTION",
545+
type: "regex"
546+
};
547+
break;
541548
} // switch()
542549

543550
return validator;

src/validation-service.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ angular
2727
validationService.prototype.addValidator = addValidator;
2828
validationService.prototype.checkFormValidity = checkFormValidity;
2929
validationService.prototype.removeValidator = removeValidator;
30+
validationService.prototype.clearValidationSummary = clearValidationSummary;
3031
validationService.prototype.setGlobalOptions = setGlobalOptions;
3132

3233
return validationService;
@@ -73,7 +74,7 @@ angular
7374
attrs = mergeObjects(self.validationAttrs, attrs);
7475

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

8586
self.commonObj.initialize(attrs.scope, attrs.elm, attrs, attrs.ctrl);
8687
attemptToValidate(self, newVal);
87-
}, true); // $watch()
88+
}, true); // $watch()
8889

8990
return self;
90-
} // addValidator()
91+
} // addValidator()
9192

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

110116
if(!!elm && elm.length > 0) {
111117
ctrl.$setTouched(); // make the element as it was touched for CSS
112-
self.commonObj.updateErrorMsg(obj.$validationSummary[i].message, {valid: false, elm: elm, submitted: true});
118+
self.commonObj.updateErrorMsg(obj.$validationSummary[i].message, { valid: false, obj: obj.$validationSummary[i].obj, submitted: true });
113119
}
114120
}
115121
return isValid;

0 commit comments

Comments
 (0)