-
Notifications
You must be signed in to change notification settings - Fork 10
/
Required.js
executable file
·40 lines (37 loc) · 1.49 KB
/
Required.js
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
(function(){
'use strict';
Required.$inject = ['$interpolate'];
function Required($interpolate) {
return {
restrict: 'A',
require: ['ngModel','^?gumgaForm'],
link: function (scope, elm, attrs, controllers) {
let error = 'required',
name = attrs.name,
field = attrs.field,
ngModel = controllers[0],
gumgaForm = controllers[1];
(function() {
let isValid = (attrs.gumgaRequired.length == 0) ?
!!(ngModel.$viewValue && ngModel.$viewValue.length > 0) :
!!($interpolate(attrs.gumgaRequired)(scope))
gumgaForm.changeStateOfInput(name, error, isValid, null, field);
ngModel.$setValidity(error, isValid);
}());
function validateRequired(inputValue) {
let isValid = (attrs.gumgaRequired.length == 0) ?
!!(inputValue && inputValue.length > 0) :
!!($interpolate(attrs.gumgaRequired)(scope))
gumgaForm.changeStateOfInput(name, error, isValid, null, field);
ngModel.$setValidity(error,isValid);
return inputValue;
};
ngModel.$parsers.unshift(validateRequired);
ngModel.$formatters.push(validateRequired);
attrs.$observe('gumgaRequired', x => validateRequired(ngModel.$viewValue));
}
}
}
angular.module('gumga.directives.form.required',[])
.directive('gumgaRequired',Required);
})();