-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidationdirectives.js
More file actions
97 lines (95 loc) · 4.12 KB
/
validationdirectives.js
File metadata and controls
97 lines (95 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
angular.module('validation', [])
.directive('customValidator', [function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
var validateFunctionNames = attr["validateFunctions"].split(",");
var validatorNames = attr["customValidator"].split(",");
ngModelCtrl.$parsers.push(function (value) {
var hasErrors = false;
angular.forEach(validateFunctionNames, function (functionName, index) {
if (!scope[functionName]) {
console.log('There is no function with name ' + functionName + ' available on the scope. Please make sure the function exists on current scope or its parent.');
} else {
var result = scope[functionName](value);
if (result && result != false) {
ngModelCtrl.$setValidity(validatorNames[index], true);
} else {
ngModelCtrl.$setValidity(validatorNames[index], false);
hasErrors = true;
}
}
});
return hasErrors ? undefined : value;
});
}
};
}])
.directive('customRemoteValidator', [function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
var validateFunctionNames = attr["remoteValidateFunctions"].split(",");
var validatorNames = attr["customRemoteValidator"].split(",");
ngModelCtrl.$parsers.push(function (value) {
angular.forEach(validateFunctionNames, function (functionName, index) {
if (!scope[functionName]) {
console.log('There is no function with ' + functionName + ' available on the scope. Please make sure the function exists on current scope or its parent.');
} else {
var result = scope[functionName](value);
if (result.then) {
result.then(function (data) { //For promise type result object
ngModelCtrl.$setValidity(validatorNames[index], data);
}, function (error) {
ngModelCtrl.$setValidity(validatorNames[index], false);
});
}
}
});
return value;
});
}
};
}])
.directive('validationMessages', function () {
return {
scope: {
modelController: '='
},
restrict: 'EA',
link: function (scope, elm, attrs) {
if (!scope.modelController) {
console.log('Requires a html attribute data-model-controller. This should point to the input field model controller.');
}
scope.$watch('modelController.$error', function (newValue) {
if (newValue) {
scope.errorMessages = [];
angular.forEach(newValue, function (value, key) {
if (value && attrs[key + 'Error']) {
scope.errorMessages.push(attrs[key + 'Error']);
}
});
}
}, true);
},
template: '<div><small class="error" ng-repeat="message in errorMessages" ng-show= "!modelController.$pristine && $first" class="warning">{{message}}</small></div>'
}
})
.directive('updateOnBlur', function () {
return {
restrict: 'A',
require: 'ngModel',
priority: '100',
link: function (scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
elm.unbind('input').unbind('keydown').unbind('change');
elm.bind('blur', function () {
scope.$apply(function () {
ngModelCtrl.$setViewValue(elm.val());
});
});
}
};
});