You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/guide/dev_guide.forms.ngdoc
+72-82Lines changed: 72 additions & 82 deletions
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,19 @@
2
2
@name Developer Guide: Forms
3
3
@description
4
4
5
-
Forms and form controls (`input`, `select`, `textarea`) are user's gateway to your application -
6
-
that's how your application accepts input from the user.
5
+
Controls (`input`, `select`, `textarea`) are a way for user to enter data.
6
+
Form is a collection of controls for the purpose of grouping related controls together.
7
7
8
-
In order to provide good user experience while gathering user input, it is important to validate
9
-
this input and give the user hints on how to correct errors. Angular provides several mechanisms
10
-
that make this easier, but keep in mind that while client-side validation plays an important role in
11
-
providing good user experience, it can be easily circumvented and thus a server-side validation is
12
-
still necessary.
8
+
Form and controls provide validation services, so that the user can be notified of invalid input.
9
+
This provides a better user experience, because the user gets instant feedback on how to correct the error.
10
+
Keep in mind that while client-side validation plays an important role in providing good user experience, it can easily be circumvented and thus can not be trusted.
11
+
Server-side validation is still necessary for a secure application.
13
12
14
13
15
14
# Simple form
16
-
The most important directive is {@link api/angular.module.ng.$compileProvider.directive.ng:model ng-model},
17
-
which tells Angular to do two-way data binding. That means, the value in the form control is
18
-
synchronized in both directions with the bound model (specified as value of `ng-model` attribute).
19
-
15
+
The key directive in understanding two-way data-binding is {@link api/angular.module.ng.$compileProvider.directive.ng-model ng-model}.
16
+
The `ng-model` provides the two-way data-binding by synchronizing the model to the view, as well as view to the model.
17
+
In addition it provides {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController API} for other directives to augment its behavior.
20
18
21
19
<doc:example>
22
20
<doc:source>
@@ -29,8 +27,6 @@ synchronized in both directions with the bound model (specified as value of `ng-
29
27
<button ng-click="reset()">RESET</button>
30
28
<button ng-click="update(user)">SAVE</button>
31
29
</form>
32
-
<!-- reading these values outside <form> scope is possible only because we defined these objects
33
-
on the parent scope, and ng-model only change properties of this object -->
34
30
<pre>form = {{user | json}}</pre>
35
31
<pre>master = {{master | json}}</pre>
36
32
</div>
@@ -54,47 +50,33 @@ synchronized in both directions with the bound model (specified as value of `ng-
54
50
</doc:example>
55
51
56
52
57
-
Note, that the `user.name` is updated immediately - that's because of
@@ -203,39 +180,51 @@ stored on the `FormController`.
203
180
204
181
205
182
206
-
# Advanced / custom validation
183
+
# Custom Validation
207
184
208
185
Angular provides basic implementation for most common html5 {@link api/angular.module.ng.$compileProvider.directive.input input}
209
-
types ({@link api/angular.module.ng.$compileProvider.directive.input.text text}, {@link api/angular.module.ng.$compileProvider.directive.input.number number}, {@link api/angular.module.ng.$compileProvider.directive.input.url url}, {@link api/angular.module.ng.$compileProvider.directive.input.email email}, {@link api/angular.module.ng.$compileProvider.directive.input.radio radio}, {@link api/angular.module.ng.$compileProvider.directive.input.checkbox checkbox}), as well as some directives for validation (`required`, `pattern`, `minlength`, `maxlength`, `min`, `max`).
186
+
types: ({@link api/angular.module.ng.$compileProvider.directive.input.text text}, {@link api/angular.module.ng.$compileProvider.directive.input.number number}, {@link api/angular.module.ng.$compileProvider.directive.input.url url}, {@link api/angular.module.ng.$compileProvider.directive.input.email email}, {@link api/angular.module.ng.$compileProvider.directive.input.radio radio}, {@link api/angular.module.ng.$compileProvider.directive.input.checkbox checkbox}), as well as some directives for validation (`required`, `pattern`, `minlength`, `maxlength`, `min`, `max`).
187
+
188
+
Defining your own validator can be done by defining your own directive which adds a custom validation function to the `ng-model` {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController controller}.
189
+
To get a hold of the controller the directive specifies a dependency as shown in the example below.
190
+
The validation can occur in two places:
210
191
211
-
However, when this is not enough for your application, you can simply define a custom directive.
212
-
This directive can require `ngModel`, which means it can't exist without `ng-model` and its linking
213
-
function gets fourth argument - an instance of `NgModelController`, which is a communication channel
214
-
to `ng-model`, that allows you to hook into the validation process.
192
+
* **Model to View update** -
193
+
Whenever the bound model changes, all functions in {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController#$formatters NgModelController#$formatters} array are pipe-lined, so that each of these functions has an opportunity to format the value and change validity state of the form control through {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController#$setValidity NgModelController#$setValidity}.
215
194
216
-
## Model to View update
217
-
Whenever the bound model changes, all functions in {@link api/angular.module.ng.$compileProvider.directive.ng:model.NgModelController#formatters NgModelController#formatters} array are pipe-lined, so that each of these functions has an opportunity to format the value and change validity state of the form control through {@link api/angualar.module.ng.$compileProvider.directive.ng:model.NgModelController#$setValidity NgModelController#$setValidity}.
195
+
* **View to Model update** -
196
+
In a similar way, whenever a user interacts with a control, the controll calls {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController#$setViewValue NgModelController#$setViewValue}.
197
+
This in turn pipelines all functions in {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController#$parsers NgModelController#$parsers} array, so that each of these functions has an opportunity to convert the value and change validity state of the form control through {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController#$setValidity NgModelController#$setValidity}.
218
198
219
-
## View to Model update
220
-
In a similar way, whenever a form control calls {@link api/angular.module.ng.$compileProvider.directive.ng:model.NgModelController#setViewValue NgModelController#setViewValue}, all functions in {@link api/angular.module.ng.$compileProvider.directive.ng:model.NgModelController#parsers NgModelController#parsers} array are pipe-lined, so that each of these functions has an opportunity to correct/convert the value and change validity state of the form control through {@link api/angualar.module.ng.$compileProvider.directive.ng:model.NgModelController#setValidity NgModelController#$setValidity}.
199
+
In the following example we create two directives.
221
200
222
-
In this example we create two simple directives. The first one is `integer` and it validates whether the input is valid integer, so for example `1.23` is an invalid value. Note, that we unshift the array instead of pushing - that's because we want to get a string value, so we need to execute the validation function before a conversion to number happens.
201
+
* The first one is `integer` and it validates whether the input is a valid integer.
202
+
For example `1.23` is an invalid value, since it contains a fraction.
203
+
Note, that we unshift the array instead of pushing.
204
+
This is because we want to be first parser and consume the control string value, as we need to execute the validation function before a conversion to number occurs.
223
205
224
-
The second directive is `smart-float`. It parses both `1.2` and `1,2` into a valid float number `1.2`. Note, we can't use input type `number` here - browser would not allow user to type invalid number such as `1,2`.
206
+
* The second directive is a `smart-float`.
207
+
It parses both `1.2` and `1,2` into a valid float number `1.2`.
208
+
Note that, we can't use input type `number` here as HTML5 browsers would not allow the user to type what it would consider an invalid number such as `1,2`.
<span ng-show="form.length.$error.FLOAT">This is not valid number!</span>
240
229
</div>
241
230
</form>
@@ -287,16 +276,17 @@ The second directive is `smart-float`. It parses both `1.2` and `1,2` into a val
287
276
288
277
289
278
# Implementing custom form control (using ng-model)
290
-
Angular has all the basic form controls implemented ({@link api/angular.module.ng.$compileProvider.directive.input input}, {@link api/angular.module.ng.$compileProvider.directive.select select}, {@link api/angular.module.ng.$compileProvider.directive.textarea textarea}), so most of the time you should be just fine with them. However, if you need more flexibility, you can write your own form control - it's gonna be a directive again.
279
+
Angular implements all of the basic HTML form controls ({@link api/angular.module.ng.$compileProvider.directive.input input}, {@link api/angular.module.ng.$compileProvider.directive.select select}, {@link api/angular.module.ng.$compileProvider.directive.textarea textarea}), which should be sufficient for most cases.
280
+
However, if you need more flexibility, you can write your own form control as a directive.
291
281
292
-
You basically need to do two things to get it working together with `ng-model` binding:
282
+
In order for custom control to work with `ng-model` and to achieve two-way data-binding it needs to:
293
283
294
-
- implement `render` method, that knows how to reflect value change to view,
295
-
- call `setViewValue` method, whenever the view value changes - that's usually inside DOM Event listener.
284
+
- implement `render` method, which is responsible for rendering the data after it passed the {@link api/angular.module.ng.$compileProvider.directive.ng-model.NgModelController#$formatters NgModelController#$formatters},
285
+
- call `$setViewValue` method, whenever the user interacts with the control and model needs to be updated. This is usually done inside a DOM Event listener.
296
286
297
287
See {@link api/angular.module.ng.$compileProvider.directive $compileProvider.directive} for more info.
298
288
299
-
This example shows how easy it is to add a support for binding contentEditable elements.
289
+
The following example shows how to add two-way data-binding to contentEditable elements.
0 commit comments