-
Notifications
You must be signed in to change notification settings - Fork 10
/
Pattern.js
executable file
·34 lines (32 loc) · 1.13 KB
/
Pattern.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
(function(){
'use strict';
Pattern.$inject = [];
function Pattern() {
return {
restrict: 'A',
require: ['ngModel', '?^gumgaForm'],
link:(scope, elm, attrs, controllers) => {
if (!attrs.gumgaPattern)throw "O valor da diretiva gumga-pattern não foi informado.";
let ngModelController = controllers[0],
gumgaFormController = controllers[1],
error = 'pattern',
name = attrs.name,
field = attrs.field,
regex = new RegExp(`^${attrs.gumgaPattern}$`);
function validatePattern (inputValue) {
if(inputValue) {
let isValid = regex.test(inputValue);
ngModelController.$setValidity(error, isValid);
gumgaFormController.changeStateOfInput(name, error, isValid, attrs.gumgaPattern, field);
}
return inputValue;
};
ngModelController.$parsers.unshift(validatePattern);
ngModelController.$formatters.push(validatePattern);
attrs.$observe('gumgaPattern', x => validatePattern(ngModelController.$viewValue));
}
}
}
angular.module('gumga.directives.form.pattern',[])
.directive('gumgaPattern',Pattern);
})();