Angular Forms
Learn how Angular forms collect user input, track form state, validate data and submit information in an application.
What are Angular Forms?
Forms are used when an application needs to collect information from users.
Examples include course enquiries, registration forms, login forms, contact forms and application forms.
Angular provides two major approaches for working with forms: Template-driven Forms and Reactive Forms.
Angular Forms Flow
Main Angular Form Concepts
Form Controls
Represent individual fields such as name, email or course.
Form State
Angular tracks whether a form is valid, dirty, touched or submitted.
Validation
Validation checks whether entered values satisfy requirements.
Submission
Valid form data can be processed when the user submits the form.
Two Approaches to Angular Forms
Template-driven Forms
Template-driven forms use Angular directives inside the HTML template to create and manage the form.
They are useful for simple forms where most of the form behavior can be described directly in the template.
<form #enquiryForm="ngForm">
<input
type="text"
name="name"
ngModel>
<input
type="email"
name="email"
ngModel>
<button type="submit">
Submit
</button>
</form>
Reactive Forms
Reactive forms create the form model in TypeScript using objects such as FormControl and FormGroup.
They provide a programmatic approach that is useful for complex forms and dynamic validation requirements.
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-enquiry',
templateUrl: './enquiry.component.html'
})
export class EnquiryComponent {
enquiryForm = new FormGroup({
name: new FormControl(''),
email: new FormControl(''),
course: new FormControl('')
});
}
<form [formGroup]="enquiryForm">
<input
type="text"
formControlName="name">
<input
type="email"
formControlName="email">
<input
type="text"
formControlName="course">
</form>
FormControl
FormControl represents a single form field and keeps track of its current value and state.
For example, an email field can be represented by one FormControl.
email = new FormControl('');
console.log(
this.email.value
);
FormGroup
FormGroup groups multiple form controls into a single form model.
It allows the application to manage related fields together.
registrationForm = new FormGroup({
name: new FormControl(''),
email: new FormControl(''),
mobile: new FormControl(''),
course: new FormControl('')
});
CIIT Training Institute Example
Suppose CIIT Training Institute has an online enquiry form where visitors enter their name, email and preferred course.
Angular can collect this information and validate it before sending the data to the backend.
<form>
<label>
Name
</label>
<input
type="text"
placeholder="Enter your name">
<label>
Email
</label>
<input
type="email"
placeholder="Enter your email">
<label>
Course
</label>
<select>
<option>
Java Full Stack
</option>
<option>
.NET Full Stack
</option>
<option>
Python Full Stack
</option>
</select>
<button type="submit">
Send Enquiry
</button>
</form>
CIIT Training Institute Enquiry
Name: Enter your name
Email: Enter your email
Course: Select Course
Form State
Angular tracks the state of form controls and forms. This information can be used to decide when to show validation messages or enable actions.
Common states include valid, invalid, touched, untouched, dirty and pristine.
Valid
Field or form satisfies its validation rules.
Invalid
One or more validation rules are not satisfied.
Touched
The user has interacted with the field and left it.
Dirty
The field value has been changed by the user.
Form Validation
Validation ensures that the entered data satisfies the application's requirements before the form is processed.
Angular provides built-in validators for common requirements such as required fields and email values.
import { Validators } from '@angular/forms';
email = new FormControl(
'',
[
Validators.required,
Validators.email
]
);
Displaying Validation Message
<input
type="email"
[formControl]="email">
<div
*ngIf="email.invalid && email.touched">
Please enter a valid email address.
</div>
Form Submission
When the user submits a form, Angular can collect the current form values and send them for further processing.
The application should normally validate the form before processing or sending its data.
submitForm() {
if (this.enquiryForm.valid) {
console.log(
this.enquiryForm.value
);
}
}
Template-driven vs Reactive Forms
| Feature | Template-driven | Reactive |
|---|---|---|
| Form Definition | Mainly in template | Mainly in TypeScript |
| Form Model | Created through directives | Explicitly created |
| Complexity | Good for simpler forms | Good for complex forms |
| Validation | Template-based validators | Programmatic validators |
| Testing | More template-oriented | Form model is easier to test independently |
Common Uses of Angular Forms
Login
Collect username or email and password.
Enquiry
Collect course and contact information.
Registration
Capture user registration information.
Contact
Collect messages and contact details.
Important Points
Use template-driven forms for simpler scenarios and reactive forms when more control is required.
Validate important fields before processing form data.
Use form state such as valid, invalid, touched and dirty to control the user experience.
Use meaningful field names and clear validation messages.
Angular Forms Best Practices
- Use meaningful names for controls and form fields.
- Validate user input before processing form data.
- Display clear and useful validation messages.
- Keep complex form logic inside the component rather than putting unnecessary logic into the template.
- Use reactive forms when the form has complex validation or dynamic controls.
- Do not submit invalid or incomplete data to the backend.
Summary
Angular Forms are used to collect, track, validate and submit user input.
Angular provides two main approaches: Template-driven Forms and Reactive Forms.
Forms can use controls, validation and form state to create reliable user-input workflows. In the next topic, we will learn about Template-driven Forms.