Professional Documents
Culture Documents
Validation ts COOKBOOK
FORM VALIDATION
We can improve overall data quality by validating user input for accuracy and
completeness.
In this cookbook we show how to validate user input in the UI and display useful validation
messages using 堀rst the template-driven forms and then the reactive forms approach.
Table of Contents
Simple Template-Driven Forms
Custom validation
Testing
Try the live example to see and download the full cookbook source code
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 1/25
11/20/2016 Form Validation ts COOKBOOK
You add Angular form directives (mostly directives beginning ng... ) to help Angular
construct a corresponding internal control model that implements form functionality. We
say that the control model is implicit in the template.
To validate user input, you add HTML validation attributes to the elements. Angular
interprets those as well, adding validator functions to the control model.
Angular exposes information about the state of the controls including whether the user
has "touched" the control or made changes and if the control values are valid.
In the 堀rst template validation example, we add more HTML to read that control state and
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 2/25
11/20/2016 Form Validation ts COOKBOOK
update the display appropriately. Here's an excerpt from the template html for a single
input box control bound to the hero name:
<label for="name">Name</label>
We set the name attribute of the input box to "name" so Angular can track this input
element and associate it with an Angular form control called name in its internal
control model.
We use the [(ngModel)] directive to two-way data bind the input box to the
hero.name property.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 3/25
11/20/2016 Form Validation ts COOKBOOK
The *ngIf on <div> element reveals a set of nested message divs but only if
there are "name" errors and the control is either dirty or touched .
Each nested <div> can present a custom message for one of the possible
validation errors. We've prepared messages for required , minlength , and
maxlength .
The full template repeats this kind of layout for each data entry control on the form.
We shouldn't show errors for a new hero before the user has had a chance to
edit the value. The checks for dirty and touched prevent premature display
of errors.
The component class manages the hero model used in the data binding as well as other
code to support the view.
template/hero-form-template1.component.ts (class)
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 4/25
11/20/2016 Form Validation ts COOKBOOK
'Dr. What');
6.
7. submitted = false;
8.
9. onSubmit() {
10. this.submitted = true;
11. }
12.
13. addHero() {
14. this.hero = new Hero(42, '', '');
15. }
16. }
Use this template-driven validation technique when working with static forms with simple,
standard validation rules.
Here are the complete 堀les for the 堀rst version of HeroFormTemplateCompononent in
the template-driven approach:
1. <div class="container">
2. <div [hidden]="submitted">
3. <h1>Hero Form 1 (Template)</h1>
4. <form #heroForm="ngForm" *ngIf="active" (ngSubmit)="onSubmit()">
5. <div class="form-group">
6. <label for="name">Name</label>
7.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 5/25
11/20/2016 Form Validation ts COOKBOOK
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 6/25
11/20/2016 Form Validation ts COOKBOOK
56. </div>
It takes a lot of HTML to represent all possible error conditions. This gets out of hand
when there are many controls and many validation rules.
The messages are static strings, hard-coded into the template. We often require
dynamic messages that we should shape in code.
We can move the logic and the messages into the component with a few changes to the
template and component.
Here's the hero name again, excerpted from the revised template ("Template 2"), next to
the original version:
1. <label for="name">Name</label>
2.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 7/25
11/20/2016 Form Validation ts COOKBOOK
The <input> element HTML is almost the same. There are noteworthy differences:
The #name template variable is gone because we no longer refer to the Angular
control for this element.
COMPONENT CLASS
The original component code stays the same. We added new code to acquire the Angular
form control and compose error messages.
The 堀rst step is to acquire the form control that Angular created from the template by
querying for it.
Look back at the top of the component template where we set the #heroForm template
variable in the <form> element:
The heroForm variable is a reference to the control model that Angular derived from the
template. We tell Angular to inject that model into the component class's currentForm
property using a @ViewChild query:
template/hero-form-template2.component.ts (heroForm)
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 8/25
11/20/2016 Form Validation ts COOKBOOK
heroForm: NgForm;
@ViewChild('heroForm') currentForm: NgForm;
ngAfterViewChecked() {
this.formChanged();
}
formChanged() {
if (this.currentForm === this.heroForm) { return; }
this.heroForm = this.currentForm;
if (this.heroForm) {
this.heroForm.valueChanges
.subscribe(data => this.onValueChanged(data));
}
}
Some observations:
Angular @ViewChild queries for a template variable when you pass it the name of
that variable as a string ( 'heroForm' in this case).
The heroForm object changes several times during the life of the component, most
notably when we add a new hero. We'll have to re-inspect it periodically.
template/hero-form-template2.component.ts (handler)
onValueChanged(data?: any) {
if (!this.heroForm) { return; }
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 9/25
11/20/2016 Form Validation ts COOKBOOK
formErrors = {
'name': '',
'power': ''
};
The onValueChanged handler interprets user data entry. The data object passed into
the handler contains the current element values. The handler ignores them. Instead, it
iterates over the 堀elds of the component's formErrors object.
The formErrors is a dictionary of the hero 堀elds that have validation rules and their
current error messages. Only two hero properties have validation rules, name and
power . The messages are empty strings when the hero data are valid.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 10/25
11/20/2016 Form Validation ts COOKBOOK
We'll need some error messages of course, a set for each validated property, one
message per validation rule:
template/hero-form-template2.component.ts (messages)
validationMessages = {
'name': {
'required': 'Name is required.',
'minlength': 'Name must be at least 4 characters long.',
'maxlength': 'Name cannot be more than 24 characters long.',
'forbiddenName': 'Someone named "Bob" cannot be a hero.'
},
'power': {
'required': 'Power is required.'
}
};
Now every time the user makes a change, the onValueChanged handler checks for
validation errors and produces messages accordingly.
Is this an improvement?
Clearly the template got substantially smaller while the component code got substantially
larger. It's not easy to see the bene堀t when there are just three 堀elds and only two of them
have validation rules.
Consider what happens as we increase the number of validated 堀elds and rules. In
general, HTML is harder to read and maintain than code. The initial template was already
large and threatening to get rapidly worse as we add more validation message <divs> .
After moving the validation messaging to the component, the template grows more slowly
and proportionally. Each 堀eld has approximately the same number of lines no matter its
number of validation rules. The component also grows proportionally, at the rate of one
line per validated 堀eld and one line per validation message.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 11/25
11/20/2016 Form Validation ts COOKBOOK
Now that the messages are in code, we have more 儀exibility. We can compose messages
more intelligently. We can refactor the messages out of the component, perhaps to a
service class that retrieves them from the server. In short, there are more opportunities to
improve message handling now that text and logic have moved from template to code.
We've been reviewing the "Template-driven" approach which requires the FormsModule
Here's how we imported it in the HeroFormTemplateModule .
template/hero-form-template.module.ts
@NgModule({
imports: [ SharedModule, FormsModule ],
declarations: [ HeroFormTemplate1Component,
HeroFormTemplate2Component ],
exports: [ HeroFormTemplate1Component,
HeroFormTemplate2Component ]
})
export class HeroFormTemplateModule { }
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 12/25
11/20/2016 Form Validation ts COOKBOOK
They're not germane to the validation story. Look at the live example if you're
interested.
Reactive Forms
In the template-driven approach, you markup the template with form elements, validation
attributes, and ng... directives from the Angular FormsModule . At runtime, Angular
interprets the template and derives its form control model.
Reactive Forms takes a different approach. You create the form control model in code.
You write the template with form elements and form... directives from the Angular
ReactiveFormsModule . At runtime, Angular binds the template elements to your control
This approach requires a bit more effort. You have to write the control model and manage
it.
The third cookbook sample re-writes the hero form in reactive forms style.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 13/25
11/20/2016 Form Validation ts COOKBOOK
app/reactive/hero-form-reactive.module.ts
@NgModule({
imports: [ SharedModule, ReactiveFormsModule ],
declarations: [ HeroFormReactiveComponent ],
exports: [ HeroFormReactiveComponent ]
})
export class HeroFormReactiveModule { }
The "Reactive Forms" feature module and component are in the app/reactive folder.
Let's focus on the HeroFormReactiveComponent there, starting with its template.
Component template
We begin by changing the <form> tag so that it binds the Angular formGroup directive
in the template to the heroForm property in the component class. The heroForm is the
control model that the component class builds and maintains.
Then we modify the template HTML elements to match the reactive forms style. Here is
the "name" portion of the template again, revised for reactive forms and compared with
the template-driven version:
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 14/25
11/20/2016 Form Validation ts COOKBOOK
1. <label for="name">Name</label>
2.
Key changes:
the validation attributes are gone (except required ) because we'll be validating in
code.
required remains, not for validation purposes (we'll cover that in the code), but
A future version of reactive forms will add the required HTML validation
attribute to the DOM element (and perhaps the aria-required attribute) when
the control has the required validator function.
Until then, apply the required attribute and add the Validator.required
function to the control model, as we'll do below.
the formControlName replaces the name attribute; it serves the same purpose of
correlating the input box with the Angular form control.
the two-way [(ngModel)] binding is gone. The reactive approach does not use data
binding to move data into and out of the form controls. We do that in code.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 15/25
11/20/2016 Form Validation ts COOKBOOK
The retreat from data binding is a principle of the reactive paradigm rather than
a technical limitation.
Component class
The component class is now responsible for de堀ning and managing the form control
model.
Angular no longer derives the control model from the template so we can no longer query
for it. We create the Angular form control model explicitly with the help of the
FormBuilder .
Here's the section of code devoted to that process, paired with the template-driven code it
replaces:
1. heroForm: FormGroup;
2. constructor(private fb: FormBuilder) { }
3.
4. ngOnInit(): void {
5. this.buildForm();
6. }
7.
8. buildForm(): void {
9. this.heroForm = this.fb.group({
10. 'name': [this.hero.name, [
11. Validators.required,
12. Validators.minLength(4),
13. Validators.maxLength(24),
14. forbiddenNameValidator(/bob/i)
15. ]
16. ],
17. 'alterEgo': [this.hero.alterEgo],
18. 'power': [this.hero.power, Validators.required]
19. });
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 16/25
11/20/2016 Form Validation ts COOKBOOK
20.
21. this.heroForm.valueChanges
22. .subscribe(data => this.onValueChanged(data));
23.
we call a buildForm method in the ngOnInit lifecycle hook method because that's
when we'll have the hero data. We'll call it again in the addHero method.
A real app would retrieve the hero asynchronously from a data service, a task
best performed in the ngOnInit hook.
the buildForm method uses the FormBuilder ( fb ) to declare the form control
model. Then it attaches the same onValueChanged handler (there's a one line
difference) to the form's valueChanged event and calls it immediately to set error
messages for the new control model.
FORMBUILDER DECLARATION
The FormBuilder declaration object speci堀es the three controls of the sample's hero
form.
Each control spec is a control name with an array value. The 堀rst array element is the
current value of the corresponding hero 堀eld. The (optional) second value is a validator
function or an array of validator functions.
Most of the validator functions are stock validators provided by Angular as static methods
of the Validators class. Angular has stock validators that correspond to the standard
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 17/25
11/20/2016 Form Validation ts COOKBOOK
In two-way data binding, the user's changes 儀ow automatically from the controls back to
the data model properties. Reactive forms do not use data binding to update data model
properties. The developer decides when and how to update the data model from control
values.
The onSubmit method simply replaces the hero object with the combined values of the
form:
onSubmit() {
this.submitted = true;
this.hero = this.heroForm.value;
}
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 18/25
11/20/2016 Form Validation ts COOKBOOK
The addHero method discards pending changes and creates a brand new hero model
object.
addHero() {
this.hero = new Hero(42, '', '');
this.buildForm();
}
Then it calls buildForm again which replaces the previous heroForm control model
with a new one. The <form> tag's [formGroup] binding refreshes the page with the
new control model.
Here's the complete reactive component 堀le, compared to the two template-driven
component 堀les.
7. @Component({
8. moduleId: module.id,
9. selector: 'hero-form-reactive3',
10. templateUrl: 'hero-form-reactive.component.html'
11. })
12. export class HeroFormReactiveComponent implements OnInit {
13.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 19/25
11/20/2016 Form Validation ts COOKBOOK
20. onSubmit() {
21. this.submitted = true;
22. this.hero = this.heroForm.value;
23. }
24. addHero() {
25. this.hero = new Hero(42, '', '');
26. this.buildForm();
27.
52. this.heroForm.valueChanges
53. .subscribe(data => this.onValueChanged(data));
54.
58.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 20/25
11/20/2016 Form Validation ts COOKBOOK
77. formErrors = {
78. 'name': '',
79. 'power': ''
80. };
81.
82. validationMessages = {
83. 'name': {
84. 'required': 'Name is required.',
85. 'minlength': 'Name must be at least 4 characters long.',
86. 'maxlength': 'Name cannot be more than 24 characters long.',
87. 'forbiddenName': 'Someone named "Bob" cannot be a hero.'
88. },
89. 'power': {
90. 'required': 'Power is required.'
91. }
92. };
93. }
Run the live example to see how the reactive form behaves and to compare all of
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 21/25
11/20/2016 Form Validation ts COOKBOOK
Custom validation
This cookbook sample has a custom forbiddenNamevalidator function that's applied
to both the template-driven and the reactive form controls. It's in the app/shared folder
and declared in the SharedModule .
shared/forbidden-name.directive.ts (forbiddenNameValidator)
The function is actually a factory that takes a regular expression to detect a speci c
forbidden name and returns a validator function.
In this sample, the forbidden name is "bob"; the validator rejects any hero name containing
"bob". Elsewhere it could reject "alice" or any name that the con堀guring regular expression
matches.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 22/25
11/20/2016 Form Validation ts COOKBOOK
'name': [this.hero.name, [
Validators.required,
Validators.minLength(4),
Validators.maxLength(24),
forbiddenNameValidator(/bob/i)
]
],
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 23/25
11/20/2016 Form Validation ts COOKBOOK
Angular forms recognizes the directive's role in the validation process because the
directive registers itself with the NG_VALIDATORS provider, a provider with an extensible
collection of validation directives.
shared/forbidden-name.directive.ts (providers)
The rest of the directive is unremarkable and we present it here without further comment.
shared/forbidden-name.directive.ts (directive)
1. @Directive({
2. selector: '[forbiddenName]',
3. providers: [{provide: NG_VALIDATORS, useExisting:
ForbiddenValidatorDirective, multi: true}]
4. })
5. export class ForbiddenValidatorDirective implements Validator, OnChanges
{
6. @Input() forbiddenName: string;
7. private valFn = Validators.nullValidator;
8.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 24/25
11/20/2016 Form Validation ts COOKBOOK
22. }
23. }
Testing Considerations
We can write isolated unit tests of validation and control logic in Reactive Forms.
Isolated unit tests probe the component class directly, independent of its interactions with
its template, the DOM, other dependencies, or Angular itself.
Such tests have minimal setup, are quick to write, and easy to maintain. They do not
require the Angular TestBed or asynchronous testing practices.
That's not possible with Template-driven forms. The template-driven approach relies on
Angular to produce the control model and to derive validation rules from the HTML
validation attributes. You must use the Angular TestBed to create component test
instances, write asynchronous tests, and interact with the DOM.
While not dif堀cult, this takes more time, work and skill — factors that tend to diminish test
code coverage and quality.
https://angular.io/docs/ts/latest/cookbook/formvalidation.html 25/25