forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.validator.asynchronous.html
87 lines (79 loc) · 3 KB
/
angular.validator.asynchronous.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
<h1>angular.validator.asynchronous</h1>
<fieldset class="workInProgress">
<legend>Work In Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.
</fieldset>
<h2>Description</h2>
<p>Use asynchronous validator if the validation can not be computed
immediately, but is provided through a callback. The widget
automatically shows a spinning indicator while the validity of
the widget is computed. This validator caches the result.</p>
<h2>Usage</h2>
<h3>In HTML Template Binding</h3>
<tt>
<input type="text" ng:validate="asynchronous:validate<i>[:update]</i>"/>
</tt>
<h3>In JavaScript</h3>
<tt ng:non-bindable>
angular.validator.asynchronous(value, validate<i>[, update]</i>);
</tt>
<h3>Parameters</h3>
<ul>
<li><tt>value</tt> –
<tt>{string}</tt>
<tt></tt>
– value to validate</li>
<li><tt>validate</tt> –
<tt>{function(inputToValidate,validationDone)}</tt>
<tt></tt>
– function to call to validate the state
of the input.</li>
<li><tt>update</tt> –
<tt>{function(data)=}</tt>
<tt>[noop]</tt>
– function to call when state of the
validator changes</li>
</ul>
<p>The <code>validate</code> function (specified by you) is called as
<code>validate(inputToValidate, validationDone)</code>:</p>
<ul>
<li><code>inputToValidate</code>: value of the input box.</li>
<li><code>validationDone</code>: <code>function(error, data){...}</code>
<ul><li><code>error</code>: error text to display if validation fails</li>
<li><code>data</code>: data object to pass to update function</li></ul></li>
</ul>
<p>The <code>update</code> function is optionally specified by you and is
called by <tt><angular/></tt> on input change. Since the
asynchronous validator caches the results, the update
function can be called without a call to <code>validate</code>
function. The function is called as <code>update(data)</code>:</p>
<ul>
<li><code>data</code>: data object as passed from validate function</li>
</ul>
<h3>CSS</h3>
ng-input-indicator-wait, ng-validation-error
<h2>Example</h2>
<doc:example>
<doc:source>
<script>
function myValidator(inputToValidate, validationDone) {
setTimeout(function(){
validationDone(inputToValidate.length % 2);
}, 500);
}
</script>
This input is validated asynchronously:
<input name="text" ng:validate="asynchronous:$window.myValidator">
</doc:source>
<doc:scenario>it('should change color in delayed way', function(){
var textBox = element('.doc-example :input');
expect(textBox.attr('className')).not().toMatch(/ng-input-indicator-wait/);
expect(textBox.attr('className')).not().toMatch(/ng-validation-error/);
input('text').enter('X');
expect(textBox.attr('className')).toMatch(/ng-input-indicator-wait/);
pause(.6);
expect(textBox.attr('className')).not().toMatch(/ng-input-indicator-wait/);
expect(textBox.attr('className')).toMatch(/ng-validation-error/);
});
</doc:scenario>
</doc:example>