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/forms.ngdoc
+47-44
Original file line number
Diff line number
Diff line change
@@ -267,35 +267,21 @@ This example shows how to debounce model changes. Model will be updated only 250
267
267
</file>
268
268
</example>
269
269
270
-
271
-
272
270
# Custom Validation
273
271
274
272
Angular provides basic implementation for most common html5 {@link ng.directive:input input}
275
273
types: ({@link input[text] text}, {@link input[number] number}, {@link input[url] url}, {@link input[email] email}, {@link input[radio] radio}, {@link input[checkbox] checkbox}), as well as some directives for validation (`required`, `pattern`, `minlength`, `maxlength`, `min`, `max`).
276
274
277
-
Defining your own validator can be done by defining your own directive which adds a custom validation function to the `ngModel` {@link ngModel.NgModelController controller}.
278
-
To get a hold of the controller the directive specifies a dependency as shown in the example below.
279
-
The validation can occur in two places:
280
-
281
-
* **Model to View update** -
282
-
Whenever the bound model changes, all functions in {@link ngModel.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 ngModel.NgModelController#$setValidity NgModelController#$setValidity}.
275
+
With a custom directive, you can add your own validation functions to the `$validators` object on the {@link ngModel.NgModelController `ngModelController`}. To get a hold of the controller, you require it in the directive as shown in the example below.
283
276
284
-
* **View to Model update** -
285
-
In a similar way, whenever a user interacts with a control it calls {@link ngModel.NgModelController#$setViewValue NgModelController#$setViewValue}.
286
-
This in turn pipelines all functions in the {@link ngModel.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 ngModel.NgModelController#$setValidity NgModelController#$setValidity}.
277
+
Each function in the `$validators` object receives the `modelValue` and the `viewValue`as parameters. Angular will then call `$setValidity` internally with the function's return value (`true`: valid, `false`: invalid). The validation functions are executed every time an input is changed (`$setViewValue` is called) or whenever the bound `model` changes. Validation happens after successfully running $parsers and $formatters, respectively. Failed validators are stored by key in `ngModelController.$error`.
287
278
288
-
In the following example we create two directives.
279
+
Additionally, there is the `$asyncValidators` object which handles asynchronous validation, such as making an `$http` request to the backend. Functions added to the object must return a promise that must be `resolved` when valid or rejected when `invalid`. In-progress async validations are added to `ngModelController.$pending`.
289
280
290
-
* The first one is `integer` and it validates whether the input is a valid integer.
291
-
For example `1.23` is an invalid value, since it contains a fraction.
292
-
Note that we unshift the array instead of pushing.
293
-
This is because we want it to be the first parser and consume the control string value, as we need to execute the validation function before a conversion to number occurs.
294
-
295
-
* The second directive is a `smart-float`.
296
-
It parses both `1.2` and `1,2` into a valid float number `1.2`.
297
-
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`.
281
+
In the following example we create two directives:
282
+
* An `integer` directive that validates whether the input is a valid integer. For example `1.23` is an invalid value, since it contains a fraction. Note that we validate the viewValue (the string value of the control), and not the modelValue. This is because input[number] converts the viewValue to a number when running the `$parsers`.
298
283
284
+
* A `username` directive that asynchronously checks if a user-entered value is already taken. We mock the server request with a `$q` deferred.
299
285
300
286
<example module="form-example1">
301
287
<file name="index.html">
@@ -304,18 +290,18 @@ In the following example we create two directives.
304
290
Size (integer 0 - 10):
305
291
<input type="number" ng-model="size" name="size"
306
292
min="0" max="10" integer />{{size}}<br />
307
-
<span ng-show="form.size.$error.integer">This is not valid integer!</span>
293
+
<span ng-show="form.size.$error.integer">The value is not a valid integer!</span>
0 commit comments