{{announcement.body}}
{{announcement.title}}

Getting Started With Angular Reactive Form Validation

DZone 's Guide to

Getting Started With Angular Reactive Form Validation

In this article, we discuss how to get started with Angular Reactive Form validation.

· Web Dev Zone ·
Free Resource

Handling user input with forms is the cornerstone of many common applications.

Applications use forms to enable users to log in, to update a profile, to enter sensitive information, and to perform many other data-entry tasks

We will be looking at how Angular handles form Validation in a Reactive Form.

Angular provides two different approaches to handling user input through forms: reactive and template-driven. Both capture user input events from the view, validate the user input, create a form model and data model to update, and provide a way to track changes.

Reactive and template-driven forms process and manage form data differently. Each offers different advantages.

Our main focus is Reactive Form:

Reactive forms provide a model-driven approach to handling form inputs whose values change over time.  It shows you how to create and update a simple form of control, progress to using multiple controls in a group, validate form values. –Angular.io

We are going to create a simple user registration form. 

Prerequisites

Navigate to the folder where you want to create your project file. Open a command window and run the command shown below.

Shell


The command above creates a new Angular application. The option to create the routing module is set to false and style files extension is set to scss.

Navigate to the new project folder and run the command to install bootstrap:

Shell


Add the following import definition in styles.scss file.

JavaScript
 




xxxxxxxxxx
1


1
@import "~bootstrap/dist/css/bootstrap.css";



Importing ReactiveFormsModule API

In order to work with Reactive Forms in Angular, we must import ReactiveFormsModule API in the app module file. 

app.module.ts

TypeScript


Use of FormGroup, ngSubmit, and formControlName in an Angular Template

There is some set of component classes that communicates with Reactive forms to manage the form data. Below is a list of some of them that we will be looking out in this project:

FormGroup FormGroup is a top-level API that maintains the values, properties and validation state of a group of AbstractControl instances in Angular.
FormBuilder It provides helpful methods to create control instances in Angular Reactive Forms.
ngSubmit This event is called when the form is submitted.
FormControl It communicates with an HTML Form element like input or select tag, this API handles the individual form value and validation state.


Required Field Validation

Let’s look at how to handle validation on each of the form fields listed above. In our project folder, open app.component.html and write the code below.

HTML


We started by creating our form, and we binded formGroup and ngSubmit classes to it in order to tracks the value and validity state of a group of FormControl instances and to submit the value when the user is done and hits the submit button. 

TypeScript


We have created our input tag, and we bind formControlName to the input to get the value when a user types. And is-invalid that we bind to our div will automatically determine if an input is good based on the rules placed on it and show an error if the user makes a mistake.

Radio Buttons in Reactive Forms

Let’s look at how we can work with Radio Buttons in our registration form.

To work with Radio Buttons in Angular application, we must set dynamic values in the form control array.

Since the user can select any values, we should use the setValue() method to assign the dynamic values to form a control array. Open app.component.html and write the below code.

HTML


In the above code, we created our radio buttons and added some default value to it but we are looking at how to set the selected value of Radio Buttons using Angular Reactive Forms.

TypeScript


To select the value from the radio button we Passed the radio button value name within the form control array. 

Using FormBuilder API

The FormBuilder service offers 3 useful methods: group(), control(), and array(). These methods generate instances in our component classes including form controls, form groups, and form arrays. Let’s look at how FormBuilder works. Open app.component.ts and write the code below.

TypeScript


We start by importing FormBuilder, FromGroup, and Validator that we used to track our form.

Then we declared registrationForm variable and assigned the value as FormGroup.

We also called this.registrationForm and passed the Value to FormBulider that will check if the values from our Form has passed the necessary requirement assign to it or not and if passed the Requirement, it will send the value to where ever we want it to but in this project, we console.log the value and if not passed, it will throw an error to the user telling them where they have made a mistake for the user to correct.

Conclusion

In this tutorial, we were able to create a registration form that we used to learn and understand how reactive form validation works in Angular. 

Get the source code from GitHub and play around with ready-made Elegant Registration Form for a better understanding.

Topics:
angular ,form validation ,reactive ,tutorial ,typescript ,web dev

Published at DZone with permission of deven rathore . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}