forked from patternfly/angular-patternfly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
167 lines (142 loc) · 4.68 KB
/
Copy pathvalidation.js
File metadata and controls
167 lines (142 loc) · 4.68 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* @ngdoc directive
* @name patternfly.validation:pfValidation
* @restrict E
* @element INPUT
* @scope
*
* @description
* Directive used for input validation based on custom function.
*
* @param {expression=} pfValidationDisabled If true, the validation is disabled, it is enabled otherwise.
*
* @example
<example module="patternfly.validation">
<file name="index.html">
<div ng-controller="ValidationDemoCtrl">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="message">Initially valid:</label>
<div class="col-sm-10">
<input class="form-control" type="text" ng-model="myValueValid" pf-validation="isNumber(input)"/>
<span class="help-block">The value you typed is not a number.</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="message">Fixed Number:</label>
<div class="col-sm-10">
<input class="form-control" type="text" ng-model="myValue" pf-validation="isNumber(input)"/>
<span class="help-block">The value you typed is not a number.</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="message">Number:</label>
<div class="col-sm-10">
<input class="form-control" type="text" ng-model="myValue" pf-validation="isNumber(input)" pf-validation-disabled="isValidationDisabled"/>
<span class="help-block">The value you typed is not a number.</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="message">Validation disabled:</label>
<div class="col-sm-10">
<input class="form-control" type="checkbox" ng-model="isValidationDisabled"/>
</div>
</div>
</form>
</div>
</file>
<file name="script.js">
angular.module( 'patternfly.validation' ).controller( 'ValidationDemoCtrl', function( $scope ) {
$scope.myValue = "Change this value to be a number";
$scope.myValueValid = 42;
$scope.isValidationDisabled = false;
$scope.isNumber = function (value) {
if (isNaN(value)) {
return false;
}
return true;
}
});
</file>
</example>
*/
angular.module('patternfly.validation', []).directive('pfValidation', function ($timeout) {
'use strict';
return {
restrict: 'A',
require: 'ngModel',
scope: {
pfValidation: '&',
pfValidationDisabled: '='
},
link: function (scope, element, attrs, ctrl) {
scope.inputCtrl = ctrl;
scope.valEnabled = !attrs.pfValidationDisabled;
scope.$watch('pfValidationDisabled', function (newVal) {
scope.valEnabled = !newVal;
if (newVal) {
scope.inputCtrl.$setValidity('pfValidation', true);
toggleErrorClass(false);
} else {
validate();
}
});
// If validation function is set
if (attrs.pfValidation) {
// using $timeout(0) to get the actual $modelValue
$timeout(function () {
validate();
}, 0);
} else if (!scope.inputCtrl.$valid && scope.inputCtrl.$dirty) {
toggleErrorClass(true);
}
scope.$watch('inputCtrl.$valid', function (isValid) {
if (isValid) {
toggleErrorClass(false);
} else {
toggleErrorClass(true);
}
});
scope.$watch('inputCtrl.$modelValue', function () {
validate();
});
function validate () {
var valid;
var val = scope.inputCtrl.$modelValue;
var valFunc = scope.pfValidation({'input': val});
if (!attrs.pfValidation) {
valFunc = true;
}
valid = !val || valFunc || val === '';
if (scope.valEnabled && !valid) {
toggleErrorClass(true);
} else {
toggleErrorClass(false);
}
}
function toggleErrorClass (add) {
var messageElement = element.next();
var parentElement = element.parent();
var hasErrorM = parentElement.hasClass('has-error');
var wasHidden = messageElement.hasClass('ng-hide');
scope.inputCtrl.$setValidity('pf-validation', !add);
if (add) {
if (!hasErrorM) {
parentElement.addClass('has-error');
}
if (wasHidden) {
messageElement.removeClass('ng-hide');
}
}
if (!add) {
if (hasErrorM) {
parentElement.removeClass('has-error');
}
if (!wasHidden) {
messageElement.addClass('ng-hide');
}
}
}
}
};
});