1. Code
  2. JavaScript
  3. Angular

Introduction to Forms in Angular 4: Reactive Forms

Scroll to top
This post is part of a series called Introduction to Forms in Angular 4.
Introduction to Forms in Angular: Template-Driven Forms
Introduction to Forms in Angular 4: Writing Custom Form Validators
Final product imageFinal product imageFinal product image
What You'll Be Creating

This is the second part of the series on Introduction to Forms in Angular 4. In the first part, we created a form using the template-driven approach. We used directives such as ngModel, ngModelGroup and ngForm to supercharge the form elements. In this tutorial, we will be taking a different approach to building forms—the reactive way. 

Reactive Forms

Reactive forms take a different approach compared to that of the template-driven forms. Here, we create and initialize the form control objects in our component class. They are intermediate objects that hold the state of the form. We will then bind them to the form control elements in the template.

The form control object listens to any change in the input control values, and they are immediately reflected in the object's state. Since the component has direct access to the data model structure, all changes can be synchronized between the data model, the form control object, and the input control values. 

High level overview of Reactive forms using model-driven approachHigh level overview of Reactive forms using model-driven approachHigh level overview of Reactive forms using model-driven approach

Practically speaking, if we are building a form for updating the user profile, the data model is the user object retrieved from the server. By convention, this is often stored inside the component's user property (this.user). The form control object or the form model will be bound to the template's actual form control elements.

Both these models should have similar structures, even if they are not identical. However, the input values shouldn't flow into the data model directly. The image describes how the user input from the template makes its way to the form model.

Let's get started.

Prerequisites

You don’t need to have followed part one of this series, for part two to make sense. However, if you are new to forms in Angular, I would highly recommend going through the template-driven strategy. The code for this project is available on my GitHub repository. Make sure that you are on the right branch and then download the zip or, alternatively, clone the repo to see the form in action. 

If you prefer to start from scratch instead, make sure that you have Angular CLI installed. Use the ng command to generate a new project. 

1
$ ng new SignupFormProject

Next, generate a new component for the SignupForm or create one manually. 

1
ng generate component SignupForm

Replace the contents of app.component.html with this:

1
<app-signup-form> </app-signup-form>

Here is the directory structure for the src/ directory. I've removed some non-essential files to keep things simple.

1
.
2
├── app
3
│   ├── app.component.css
4
│   ├── app.component.html
5
│   ├── app.component.ts
6
│   ├── app.module.ts
7
│   ├── signup-form
8
│   │   ├── signup-form.component.css
9
│   │   ├── signup-form.component.html
10
│   │   └── signup-form.component.ts
11
│   └── User.ts
12
├── index.html
13
├── main.ts
14
├── polyfills.ts
15
├── styles.css
16
├── tsconfig.app.json
17
└── typings.d.ts

As you can see, a directory for the SignupForm component has been created automatically. That's where most of our code will go. I've also created a new User.ts for storing our User model.

The HTML Template

Before we dive into the actual component template, we need to have an abstract idea of what we are building. So here is the form structure that I have in my mind. The signup form will have several input fields, a select element, and a checkbox element. 

The HTML TemplateThe HTML TemplateThe HTML Template

Here is the HTML template that we will be using for our registration page. 

HTML Template

1
 <div class="row custom-row">
2
  <div class= "col-sm-5 custom-container jumbotron">
3
      
4
    <form class="form-horizontal">
5
        <fieldset>
6
    	  <legend>SignUp</legend>
7
        
8
            <!--- Email Block --->
9
            <div class="form-group">
10
    	      <label for="inputEmail">Email</label>
11
    		  <input type="text"
12
                id="inputEmail"
13
    	        placeholder="Email">
14
    	   	</div>
15
            <!--- Password Block --->
16
    	   	<div class="form-group">
17
    	      <label for="inputPassword">Password</label>
18
    	      <input type="password" 
19
                id="inputPassword"
20
                placeholder="Password">
21
    	    </div>
22
    
23
    	    <div class="form-group">
24
    	      <label for="confirmPassword" >Confirm Password</label>
25
    	      <input type="password" 
26
                id="confirmPassword"
27
                placeholder="Password">
28
    	    </div>
29
            
30
            <!--- Select gender Block --->
31
    	    <div class="form-group">
32
    	      <label for="select">Gender</label>
33
    	        <select id="select">
34
    	          <option>Male</option>
35
    	          <option>Female</option>
36
    	          <option>Other</option>
37
    	        </select>
38
    	    </div>
39
            
40
            <!--- Terms and conditions Block --->
41
             <div class="form-group checkbox">
42
              <label>
43
                <input type="checkbox"> Confirm that you've read the Terms and 
44
                Conditions
45
              </label>
46
            </div>
47
    	   
48
           <!--- Buttons Block --->
49
    	    <div class="form-group">
50
    	        <button type="reset" class="btn btn-default">Cancel</button>
51
    	        <button type="submit" class="btn btn-primary">Submit</button>
52
    	    </div>
53
    	</fieldset>
54
    </form>
55
  </div>
56
</div>

The CSS classes used in the HTML template are part of the Bootstrap library used for making things pretty. Since this is a not a design tutorial, I won't be talking much about the CSS aspects of the form unless necessary. 

Basic Form Setup

To create a Reactive form, you need to import  the ReactiveFormsModule from @angular/forms and add it to the imports array in app.module.ts.

app/app.module.ts

1
// Import ReactiveFormsModule

2
import { ReactiveFormsModule } from '@angular/forms';
3
4
@NgModule({
5
  .
6
  .
7
  //Add the module to the imports Array

8
  imports: [
9
    BrowserModule,
10
    ReactiveFormsModule
11
 .
12
 .
13
})
14
export class AppModule { }

Next, create a User model for the registration form. We can either use a class or an interface for creating the model. For this tutorial, I am going to export a class with the following properties.

app/User.ts

1
export class User {
2
3
    id: number;
4
    email: string;
5
    //Both the passwords are in a single object

6
    password: { 
7
	  pwd: string;
8
	  confirmPwd: string;
9
	};
10
    
11
	gender: string;
12
    terms: boolean;
13
14
	constructor(values: Object = {}) {
15
	  //Constructor initialization

16
      Object.assign(this, values);
17
  }
18
19
}

Now, create an instance of the User model in the SignupForm component. 

app/signup-form/signup-form.component.ts

1
import { Component, OnInit } from '@angular/core';
2
// Import the User model

3
import { User } from './../User';
4
5
@Component({
6
  selector: 'app-signup-form',
7
  templateUrl: './signup-form.component.html',
8
  styleUrls: ['./signup-form.component.css']
9
})
10
export class SignupFormComponent implements OnInit {
11
12
  //Gender list for the select control element

13
  private genderList: string[];
14
  //Property for the user

15
  private user:User;
16
17
  ngOnInit() {
18
19
    this.genderList =  ['Male', 'Female', 'Others'];
20
  
21
   
22
}

For the signup-form.component.html file, I am going to use the same HTML template discussed above, but with minor changes. The signup form has a select field with a list of options. Although that works, we will do it the Angular way by looping through the list using the ngFor directive.

app/signup-form/signup-form.component.html

1
<div class="row custom-row">
2
  <div class= "col-sm-5 custom-container jumbotron">
3
      
4
    <form class="form-horizontal">
5
        <fieldset>
6
          <legend>SignUp</legend>
7
.
8
.
9
            <!--- Gender Block -->
10
            <div class="form-group">
11
              <label for="select">Gender</label>
12
                   <select id="select">
13
        	         
14
        	         <option *ngFor = "let g of genderList" 
15
        	           [value] = "g"> {{g}} 
16
        	         </option>
17
        	       </select>
18
        	   </div>
19
.
20
.
21
    </fieldset>
22
    </form>
23
  </div>
24
</div>

Note: You might get an error that says No provider for ControlContainer. The error appears when a component has a <form> tag without a formGroup directive. The error will disappear once we add a FormGroup directive later in the tutorial.

We have a component, a model, and a form template at hand. What now? It's time to get our hands dirty and become acquainted with the APIs that you need to create reactive forms. This includes FormControl and FormGroup

Tracking the State Using FormControl

While building forms with the reactive forms strategy, you won't come across the ngModel and ngForm directives. Instead, we use the underlying FormControl and FormGroup API.

A FormControl is a directive used to create a FormControl instance that you can use to keep track of a particular form element's state and its validation status. This is how FormControl works:

1
/* Import FormControl first */
2
import { FormControl } from '@angular/forms';
3
4
/* Example of creating a new FormControl instance */
5
export class SignupFormComponent {
6
  email = new FormControl();
7
}

email is now a FormControl instance, and you can bind it to an input control element in your template as follows:

1
<h2>Signup</h2>
2
3
<label class="control-label">Email:
4
  <input class="form-control" [formControl]="email">
5
</label>

The template form element is now bound to the FormControl instance in the component. What that means is any change to the input control value gets reflected at the other end. 

A FormControl constructor accepts three arguments—an initial value, an array of sync validators, and an array of async validators—and as you might have guessed, they are all optional. We will be covering the first two arguments here. 

1
import { Validators } from '@angular/forms';
2
.
3
.
4
.
5
/* FormControl with initial value and a validator */
6
7
  email = new FormControl('bob@example.com', Validators.required);

Angular has a limited set of built-in validators. The popular validator methods include Validators.required, Validators.minLength, Validators.maxlength, and Validators.pattern. However, to use them, you have to import the Validator API first.

For our signup form, we have multiple input control fields (for email and password), a selector field, and a checkbox field. Rather than creating individual FormControl objects, wouldn't it make more sense to group all these FormControls under a single entity? This is beneficial because we can now track the value and the validity of all the sub-FormControl objects in one place. That's what FormGroup is for. So we will register a parent FormGroup with multiple child FormControls. 

Group Multiple FormControls With FormGroup

To add a FormGroup, import it first. Next, declare signupForm as a class property and initialize it as follows:

app/signup-form/signup-form.component.ts

1
//Import the API for building a form

2
import { FormControl, FormGroup, Validators } from '@angular/forms';
3
4
5
export class SignupFormComponent implements OnInit {
6
    
7
    genderList: String[];
8
    signupForm: FormGroup;
9
    .
10
    .
11
12
   ngOnInit() {
13
14
    this.genderList =  ['Male', 'Female', 'Others'];
15
16
    this.signupForm = new FormGroup ({
17
    	email: new FormControl('',Validators.required),
18
		pwd: new FormControl(),
19
		confirmPwd: new FormControl(),
20
		gender: new FormControl(),
21
		terms: new FormControl()
22
	})
23
  
24
   }
25
}

Bind the FormGroup model to the DOM as follows: 

app/signup-form/signup-form.component.html

1
    <form class="form-horizontal"  [formGroup]="signupForm" >
2
        <fieldset>
3
    	  <legend>SignUp</legend>

4
        
5
            <!--- Email Block -->
6
            <div class="form-group">
7
    	      <label for="inputEmail">Email</label>

8
    		  <input type="text" formControlName = "email"
9
                id="inputEmail"
10
    	        placeholder="Email">
11
            
12
            .
13
            .
14
        
15
        </fieldset>

16
    </form>

[formGroup] = "signupForm" tells Angular that you want to associate this form with the FormGroup declared in the component class. When Angular sees formControlName="email", it checks for an instance of FormControl with the key value email inside the parent FormGroup. 

Similarly, update the other form elements by adding a formControlName="value" attribute as we just did here.

To see if everything is working as expected, add the following after the form tag:

app/signup-form/signup-form.component.html

1
<!--- Log the FormGroup values to see if the binding is working -->
2
    <p>Form value {{ signupForm.value | json }} </p>
3
     <p> Form status {{ signupForm.status | json}} </p>

Pipe the SignupForm property through the JsonPipe to render the model as JSON in the browser. This is helpful for debugging and logging. You should see a JSON output like this.

Form state and validty in Model-driven formsForm state and validty in Model-driven formsForm state and validty in Model-driven forms

There are two things to note here:

  1. The JSON doesn't exactly match the structure of the user model that we created earlier. 
  2. The signupForm.status displays that the status of the form is INVALID. This clearly shows that the Validators.required on the email control field is working as expected. 

The structure of the form model and the data model should match. 

1
// Form model

2
 { 
3
    "email": "", 
4
    "pwd": "", 
5
    "confirmPwd": "", 
6
    "gender": "", 
7
    "terms": false 
8
}
9
10
//User model

11
{
12
    "email": "",
13
    "password": { 
14
	  "pwd": "",
15
	  "confirmPwd": "",
16
	},
17
	"gender": "",
18
    "terms": false
19
}

To get the hierarchical structure of the data model, we should use a nested FormGroup. Additionally, it's always a good idea to have related form elements under a single FormGroup. 

Nested FormGroup

Create a new FormGroup for the password.

app/signup-form/signup-form.component.ts

1
    this.signupForm = new FormGroup ({
2
    	email: new FormControl('',Validators.required),
3
		password: new FormGroup({
4
			pwd: new FormControl(),
5
			confirmPwd: new FormControl()
6
		}),
7
		gender: new FormControl(),
8
		terms: new FormControl()
9
	})

Now, to bind the new form model with the DOM, make the following changes:

app/signup-form/signup-form.component.html

1
<!--- Password Block -->
2
    <div formGroupName = "password">
3
	   	<div class="form-group">
4
	      <label for="inputPassword">Password</label>
5
	      <input type="password" formControlName = "pwd"
6
            id="inputPassword"
7
            placeholder="Password">
8
	    </div>
9
10
	    <div class="form-group">
11
	      <label for="confirmPassword" >Confirm Password</label>
12
	      <input type="password" formControlName = "confirmPwd"
13
            id="confirmPassword"
14
            placeholder="Password">
15
	    </div>
16
    </div>

formGroupName = "password" performs the binding for the nested FormGroup. Now, the structure of the form model matches our requirements.

1
Form value: { 
2
    "email": "", "

3
    password": { "pwd": null, "confirmPwd": null }, 
4
    "gender": null, 
5
    "terms": null 
6
    }
7
8
Form status "INVALID"

Next up, we need to validate the form controls.

Validating the Form

We have a simple validation in place for the email input control. However, that's not sufficient. Here is the entire list of our requirements for the validation.

  • All form control elements are required.
  • Disable the submit button until the status of the form is VALID.
  • The email field should strictly contain an email id.
  • The password field should have a minimum length of 8.
Validating the formValidating the formValidating the form

The first one is easy. Add Validator.required to all the FormControls in the form model. 

app/signup-form/signup-form.component.ts 

1
    
2
    this.signupForm = new FormGroup ({
3
		email: new FormControl('',Validators.required),
4
		password: new FormGroup({
5
			pwd: new FormControl('', Validators.required),
6
			confirmPwd: new FormControl('', Validators.required)
7
		}),
8
		gender: new FormControl('', Validators.required),
9
        //requiredTrue so that the terms field isvalid only if checked

10
		terms: new FormControl('', Validators.requiredTrue)
11
	})

Next, disable the button while the form is INVALID.

app/signup-form/signup-form.component.html

1
<!--- Buttons Block -->
2
    <div class="form-group">
3
        <button type="reset" class="btn btn-default">Cancel</button>
4
        <button type="submit" [disabled] = "!signupForm.valid" class="btn btn-primary">Submit</button>
5
    </div>

To add a constraint on email, you can either use the default Validators.email or create a custom Validators.pattern() that specifies regular expressions like the one below:

1
email: new FormControl('',
2
    [Validators.required, 
3
    Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$')])

Use the minLength validator for the password fields.

1
    password: new FormGroup({
2
    		pwd: new FormControl('', [Validators.required, Validators.minLength(8)]),
3
			confirmPwd: new FormControl('', [Validators.required, Validators.minLength(8)])
4
		}),

That's it for the validation. However, the form model logic appears cluttered and repetitive. Let's clean that up first. 

Refactoring the Code Using FormBuilder

Angular provides you with a syntax sugar for creating new instances of FormGroup and FormControl called FormBuilder. The FormBuilder API doesn't do anything special other than what we've covered here.

It simplifies our code and makes the process of building a form easy on the eyes. To create a FormBuilder, you have to import it into signup-form.component.ts and inject the FormBuilder into the constructor.

app/signup-form/signup-form.component.ts 

1
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
2
.
3
.
4
export class SignupFormComponent implements OnInit {
5
    signupForm: FormGroup; // Declare the signupForm 

6
7
    //Inject the formbuilder into the constructor

8
	constructor(private fb:FormBuilder) {}
9
    
10
    ngOnInit() {
11
    
12
    ...
13
        
14
    }
15
16
}

Instead of a creating a new FormGroup(), we are using this.fb.group to build a form. Except for the syntax, everything else remains the same.

app/signup-form/signup-form.component.ts 

1
		
2
	ngOnInit() {
3
        ...
4
        
5
		this.signupForm  = this.fb.group({
6
			email: ['',[Validators.required,
7
						Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$')]],
8
			password: this.fb.group({
9
				pwd: ['', [Validators.required, 
10
						   Validators.minLength(8)]],
11
				confirmPwd: ['', [Validators.required,
12
								  Validators.minLength(8)]]
13
			}),
14
			gender: ['', Validators.required],
15
			terms: ['', Validators.requiredTrue]
16
		})
17
}

Displaying Validation Errors 

For displaying the errors, I am going to use the conditional directive ngIf on a div element. Let's start with the input control field for email:

1
<!-- Email error block -->
2
<div *ngIf="signupForm.controls.email.invalid && signupForm.controls.email.touched"
3
    Email is invalid
4
</div>

There are a couple of issues here. 

  1. Where did invalid and pristine come from? 
  2. signupForm.controls.email.invalid is too long and deep.
  3. The error doesn't explicitly say why it's invalid.

To answer the first question, each FormControl has certain properties like invalid, valid, pristine, dirty, touched, and untouched. We can use these to determine whether an error message or a warning should be displayed or not. The image below describes each of those properties in detail.

 FormControl Properties in Angular Reactive approach FormControl Properties in Angular Reactive approach FormControl Properties in Angular Reactive approach

So the div element with the *ngIf will be rendered only if the email is invalid. However, the user will be greeted with errors about the input fields being blank even before they have a chance to edit the form. 

To avoid this scenario, we've added the second condition. The error will be displayed only after the control has been visited.

To get rid of the long chain of method names (signupForm.controls.email.invalid), I am going to add a couple of shorthand getter methods. This keeps them more accessible and short. 

app/signup-form/signup-form.component.ts 

1
export class SignupFormComponent implements OnInit {
2
...
3
4
    get email() { return this.signupForm.get('email'); }
5
    
6
	get password() { return this.signupForm.get('password'); }
7
8
	get gender() { return this.signupForm.get('gender'); }
9
10
	get terms() { return this.signupForm.get('terms'); }
11
    
12
}

To make the error more explicit, I've added nested ngIf conditions below:

app/signup-form/signup-form.component.html

1
<!-- Email error block -->
2
	<div *ngIf="email.invalid && email.touched"
3
	 	class="col-sm-3 text-danger">
4
5
	 	<div *ngIf = "email.errors?.required">
6
	 		Email field can't be blank
7
	 	</div>
8
9
	 	<div *ngIf = "email.errors?.pattern">
10
	 		The email id doesn't seem right
11
	 	</div>
12
13
	 </div>

We use email.errors to check all possible validation errors and then display them back to the user in the form of custom messages. Now, follow the same procedure for the other form elements. Here is how I've coded the validation for the passwords and the terms input control.

app/signup-form/signup-form.component.html

1
 <!-- Password error block -->
2
       <div *ngIf="(password.invalid && password.touched)"
3
 		class="col-sm-3 text-danger">
4
 	
5
 		Password needs to be more than 8 characters
6
  	</div>
7
      
8
.
9
.
10
.
11
 <!--- Terms error block -->
12
   	  <div *ngIf="(terms.invalid && terms.touched)"
13
	 	class="col-sm-3 text-danger">
14
	 	
15
 		Please accept the Terms and conditions first.
16
   	  </div>
17
   	</div>

Submit the Form Using ngSubmit

We are nearly done with the form. It lacks the submit functionality, which we are about to implement now.

1
<form class="form-horizontal"  
2
    [formGroup]="signupForm" 
3
    (ngSubmit)="onFormSubmit()" >

On form submit, the form model values should flow into the component's user property.

app/signup-form/signup-form.component.ts

1
public onFormSubmit() {
2
    	if(this.signupForm.valid) {
3
			this.user = this.signupForm.value;
4
			console.log(this.user);
5
            /* Any API call logic via services goes here */
6
		}
7
	}

Wrapping It Up

If you've been following this tutorial series from the start, we had a hands-on experience with two popular form building technologies in Angular. The template-driven and model-driven techniques are two ways of achieving the same thing. Personally, I prefer to use the reactive forms for the following reasons:

  • All the form validation logic will be located in a single place—inside your component class. This is way more productive than the template approach, where the ngModel directives are scattered across the template.
  • Unlike template-driven forms, Model-driven forms are easier to test. You don't have to resort to end-to-end testing libraries to test your form.
  • Validation logic will go inside the component class and not in the template.
  • For a form with a large number of form elements, this approach has something called FormBuilder to make the creation of FormControl objects easier.

We missed out on one thing, and that is writing a validator for the password mismatch. In the final part of the series, we will cover everything you need to know about creating custom validator functions in Angular. Stay tuned until then.

In the meantime, there are plenty of frameworks and libraries to keep you busy, with lots of items on Envato Market to read, study, and use.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.