This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
218 lines (161 loc) · 6.34 KB
/
index.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<title>Angular Async Validation | codekraft-studio</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;
}
header {
background-color: #159957;
background-image: linear-gradient(120deg, #155799, #159957);
color: whitesmoke;
margin-bottom: 40px;
}
header .container {
padding: 40px 0;
}
header h1, h2 {
margin: 0 0 0.2em 0;
}
h2 {
margin: 0 0 0.5em 0;
}
input.ng-pending {
border: thin solid #ffc107 !important;
}
input.ng-invalid-async-validator {
border: thin solid #f44336 !important;
}
input.ng-valid-async-validator {
border: thin solid #4caf50 !important;
}
button[disabled], html input[disabled] {
cursor: not-allowed;
}
.limit-height {
max-height: 300px;
overflow: auto;
}
.glyphicon-refresh-animate {
-animation: spin .7s infinite linear;
-ms-animation: spin .7s infinite linear;
-webkit-animation: spinw .7s infinite linear;
-moz-animation: spinm .7s infinite linear;
}
@keyframes spin {
from { transform: scale(1) rotate(0deg);}
to { transform: scale(1) rotate(360deg);}
}
@-webkit-keyframes spinw {
from { -webkit-transform: rotate(0deg);}
to { -webkit-transform: rotate(360deg);}
}
@-moz-keyframes spinm {
from { -moz-transform: rotate(0deg);}
to { -moz-transform: rotate(360deg);}
}
</style>
</head>
<body ng-controller="MainCtrl" ng-cloak>
<header>
<div class="container">
<h1>angular-async-validation</h1>
<h2>the multi purpose async validation directive.</h2>
</div>
</header>
<div class="container">
<div class="row">
<div class="col-md-6">
<h2>Validation Example</h2>
<p>Enter a username to attempt the validation process.</p>
<form name="FirstForm" ng-submit="addUser()">
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
name="username"
ng-model="myModel"
placeholder="Enter your username"
async-validation="myValidator"
required
/>
<span class="input-group-btn">
<button class="btn btn-default" ng-class="{
'btn-success': FirstForm.$valid,
'btn-danger': FirstForm.$dirty && FirstForm.username.$invalid && !FirstForm.username.$error.required
}" type="submit" ng-disabled="FirstForm.$invalid || FirstForm.$pending">
<span ng-show="FirstForm.$valid" class="glyphicon glyphicon-plus"></span>
<span ng-show="FirstForm.$dirty && FirstForm.username.$invalid && !FirstForm.username.$error.required" class="glyphicon glyphicon-remove"></span>
<span ng-show="FirstForm.$pending" class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></span>
{{ FirstForm.$pending ? 'WAIT' : 'ADD' }}
</button>
</span>
</div>
</div>
</form>
<div class="list-group">
<p>Already used usernames that will cause <b>validation to fail</b>.</p>
<li class="list-group-item" ng-repeat="username in usernames">{{username}}</li>
</div>
<h2>Backend Validation</h2>
<p>If you try this input, it will check to a simulated backend API if the email already exists.</p>
<p>The only email that will fail the test is this: <strong>[email protected]</strong> all the other emails will resolve the validation.</p>
<form class="form" name="SecondForm">
<div class="form-group">
<input type="email" class="form-control" name="email" ng-model="otherModel" placeholder="Enter your email" async-validation="'api/test/:value'" required />
<div style="margin-top: 5px;">
<div class="alert alert-danger" ng-show="SecondForm.email.$error.asyncValidator">This email is already taken.</div>
<div class="alert alert-success" ng-show="SecondForm.email.$valid">This email is free to use.</div>
</div>
</div>
</form>
</div>
<div class="col-md-6">
<div class="panel">
<h2>Form Errors</h2>
<pre class="limit-height">{{ FirstForm.$error | json }}</pre>
</div>
<div class="panel">
<h2>Model Controller</h2>
<pre class="limit-height">{{ FirstForm.username | json }}</pre>
</div>
</div>
</div>
</div>
<script src="//localhost:35729/livereload.js?snipver=1"></script></body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script type="text/javascript" src="dist/angular-async-validation.min.js"></script>
<script>
angular.module('app', ['angular-async-validation']);
angular.module('app')
.controller('MainCtrl', function($scope, $q, $timeout) {
$scope.myModel = '';
$scope.usernames = ['pippo', 'franco', 'bansky', 'skizo'];
$scope.myValidator = function(modelValue, viewValue) {
// get the current value
var value = modelValue || viewValue;
var deferred = $q.defer();
if( value === '' ) {
deferred.resolve();
} else {
$timeout(function() {
if( $scope.usernames.indexOf(value) === -1 ) {
deferred.resolve();
} else {
deferred.reject();
}
}, 1000);
}
return deferred.promise;
};
$scope.addUser = function() {
$scope.usernames.push($scope.myModel);
$scope.myModel = '';
};
});
</script>
</html>