Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e9dcec0

Browse files
committed
docs(guide/forms, input): add information how to modify built-in validators
Closes #5757 Closes #7798
1 parent d5457bb commit e9dcec0

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

docs/content/guide/forms.ngdoc

+44
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,50 @@ In the following example we create two directives:
384384
</file>
385385
</example>
386386

387+
# Modifying built-in validators
388+
389+
Since Angular itself uses `$validators`, you can easily replace or remove built-in validators,
390+
should you find it necessary. The following example shows you how to overwrite the email validator
391+
in `input[email]` from a custom directive so that it requires a specific top-level domain,
392+
`example.com` to be present.
393+
Note that you can alternatively use `ng-pattern` to further restrict the validation.
394+
395+
<example module="form-example-modify-validators">
396+
<file name="index.html">
397+
<form name="form" class="css-form" novalidate>
398+
<div>
399+
Overwritten Email:
400+
<input type="email" ng-model="myEmail" overwrite-email name="overwrittenEmail" />
401+
<span ng-show="form.overwrittenEmail.$error.email">This email format is invalid!</span><br>
402+
Model: {{myEmail}}
403+
</div>
404+
</form>
405+
</file>
406+
407+
<file name="script.js">
408+
var app = angular.module('form-example-modify-validators', []);
409+
410+
app.directive('overwriteEmail', function() {
411+
var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i;
412+
413+
return {
414+
require: 'ngModel',
415+
restrict: '',
416+
link: function(scope, elm, attrs, ctrl) {
417+
// only apply the validator if ngModel is present and Angular has added the email validator
418+
if (ctrl && ctrl.$validators.email) {
419+
420+
// this will overwrite the default Angular email validator
421+
ctrl.$validators.email = function(modelValue) {
422+
return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
423+
};
424+
}
425+
}
426+
};
427+
});
428+
</file>
429+
</example>
430+
387431

388432
# Implementing custom form controls (using `ngModel`)
389433
Angular implements all of the basic HTML form controls ({@link ng.directive:input input}, {@link ng.directive:select select}, {@link ng.directive:textarea textarea}), which should be sufficient for most cases.

src/ng/directive/input.js

+12
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,12 @@ var inputType = {
651651
* Text input with URL validation. Sets the `url` validation error key if the content is not a
652652
* valid URL.
653653
*
654+
* <div class="alert alert-warning">
655+
* **Note:** `input[url]` uses a regex to validate urls that is derived from the regex
656+
* used in Chromium. If you need stricter validation, you can use `ng-pattern` or modify
657+
* the built-in validators (see the {@link guide/forms Forms guide})
658+
* </div>
659+
*
654660
* @param {string} ngModel Assignable angular expression to data-bind to.
655661
* @param {string=} name Property name of the form under which the control is published.
656662
* @param {string=} required Sets `required` validation error key if the value is not entered.
@@ -728,6 +734,12 @@ var inputType = {
728734
* Text input with email validation. Sets the `email` validation error key if not a valid email
729735
* address.
730736
*
737+
* <div class="alert alert-warning">
738+
* **Note:** `input[email]` uses a regex to validate email addresses that is derived from the regex
739+
* used in Chromium. If you need stricter validation (e.g. requiring a top-level domain), you can
740+
* use `ng-pattern` or modify the built-in validators (see the {@link guide/forms Forms guide})
741+
* </div>
742+
*
731743
* @param {string} ngModel Assignable angular expression to data-bind to.
732744
* @param {string=} name Property name of the form under which the control is published.
733745
* @param {string=} required Sets `required` validation error key if the value is not entered.

0 commit comments

Comments
 (0)