Angular Form Submission
Learn how to submit Angular forms, validate the data, access form values and process submitted information using template-driven and reactive forms.
What is Form Submission?
Form submission is the process of collecting the values entered by the user and processing them after the form is submitted.
Angular provides different ways to handle form submission depending on whether the application uses template-driven or reactive forms.
Before processing the form, the application should check whether the submitted data satisfies the required validation rules.
Form Submission Flow
Main Submission Concepts
Submit Event
Detect when the user submits the form.
Validation
Check whether the form is valid before processing.
Form Values
Access the values entered by the user.
Processing
Save the data or send it to another service.
Template-driven Form Submission
Template-driven forms commonly use the ngSubmit event to handle submission.
A template reference variable can provide access to the complete form and its current state.
<form
#enquiryForm="ngForm"
(ngSubmit)="submitForm(enquiryForm)">
<input
type="text"
name="name"
ngModel
required>
<input
type="email"
name="email"
ngModel
required
email>
<button type="submit">
Submit Enquiry
</button>
</form>
Handling Template-driven Submission
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-enquiry',
templateUrl: './enquiry.component.html'
})
export class EnquiryComponent {
submitForm(form: NgForm) {
if (form.valid) {
console.log(
form.value
);
}
}
}
Validate Before Submission
The form should normally be checked before its values are processed.
When the form is invalid, the application can display validation messages instead of processing incomplete data.
submitForm(form: NgForm) {
if (form.invalid) {
return;
}
console.log(
form.value
);
}
Tracking Submission State
Template-driven forms expose submission state through the form object.
The submitted state can be used to determine whether the form submission event has occurred.
<p>
Form Submitted:
{{ enquiryForm.submitted }}
</p>
Reactive Form Submission
Reactive forms can handle submission using the normal Angular ngSubmit event while the form data is managed by a FormGroup.
The FormGroup provides direct access to the form model and its validation state.
<form
[formGroup]="registrationForm"
(ngSubmit)="submitForm()">
<input
type="text"
formControlName="name">
<input
type="email"
formControlName="email">
<button type="submit">
Register
</button>
</form>
Handling Reactive Form Submission
import { Component } from '@angular/core';
import {
FormBuilder,
Validators
} from '@angular/forms';
@Component({
selector: 'app-registration',
templateUrl: './registration.component.html'
})
export class RegistrationComponent {
registrationForm = this.fb.group({
name: [
'',
Validators.required
],
email: [
'',
[
Validators.required,
Validators.email
]
],
course: [
'',
Validators.required
]
]);
constructor(
private fb: FormBuilder
) {
}
submitForm() {
if (this.registrationForm.invalid) {
return;
}
console.log(
this.registrationForm.value
);
}
}
Accessing Submitted Form Values
After validation succeeds, the application's component can access the current form values.
These values can then be passed to a service or another application layer for further processing.
const data =
this.registrationForm.value;
console.log(data);
Submitted Data
Name: Pradnya
Email: pradnya@example.com
Course: Angular Full Stack
CIIT Training Institute Example
Suppose a visitor submits a course enquiry form on the CIIT Training Institute website.
Angular validates the form first. If the information is valid, the component can process the enquiry data.
import { Component } from '@angular/core';
import {
FormBuilder,
Validators
} from '@angular/forms';
@Component({
selector: 'app-course-enquiry',
templateUrl: './course-enquiry.component.html'
})
export class CourseEnquiryComponent {
enquiryForm = this.fb.group({
name: [
'',
Validators.required
],
email: [
'',
[
Validators.required,
Validators.email
]
],
mobile: [
'',
Validators.required
],
course: [
'',
Validators.required
]
});
constructor(
private fb: FormBuilder
) {
}
submitEnquiry() {
if (this.enquiryForm.invalid) {
this.enquiryForm.markAllAsTouched();
return;
}
const enquiryData =
this.enquiryForm.value;
console.log(
'CIIT Enquiry:',
enquiryData
);
}
}
<form
[formGroup]="enquiryForm"
(ngSubmit)="submitEnquiry()">
<label>
Name
</label>
<input
type="text"
formControlName="name">
<label>
Email
</label>
<input
type="email"
formControlName="email">
<label>
Mobile
</label>
<input
type="text"
formControlName="mobile">
<label>
Course
</label>
<select
formControlName="course">
<option value="">
Select Course
</option>
<option value="Angular Full Stack">
Angular Full Stack
</option>
<option value="Java Full Stack">
Java Full Stack
</option>
<option value=".NET Full Stack">
.NET Full Stack
</option>
</select>
<button
type="submit"
[disabled]="enquiryForm.invalid">
Send Enquiry
</button>
</form>
CIIT Training Institute
Form Status: Valid
Course: Angular Full Stack
Submission: Enquiry Data Ready for Processing
Sending Form Data to a Service
In a real application, submitted form data is often passed to a service that communicates with the backend API.
Keeping this communication inside a service helps separate form handling from API-related logic.
submitForm() {
if (this.registrationForm.invalid) {
return;
}
const data =
this.registrationForm.value;
this.registrationService
.register(data)
.subscribe({
next: response => {
console.log(
'Registration successful',
response
);
},
error: error => {
console.error(
'Registration failed',
error
);
}
});
}
Handling Successful Submission
After successful processing, the application can display a confirmation message or clear the form.
The exact behavior depends on the application's requirements.
submitForm() {
if (this.registrationForm.invalid) {
return;
}
const data =
this.registrationForm.value;
// Process data here
console.log(
'Submitted:',
data
);
this.registrationForm.reset();
}
Reset Form After Submission
The form can be reset after successful processing when the application requires a fresh form.
this.registrationForm.reset();
Prevent Invalid Submission
submitForm() {
if (this.registrationForm.invalid) {
this.registrationForm
.markAllAsTouched();
return;
}
// Continue with valid data
}
Submission Rule
Invalid form: Do not process
Valid form: Process submitted data
Form Submission Steps
Collect
User enters information into the form.
Validate
Angular checks the configured validation rules.
Submit
The ngSubmit event calls the component method.
Process
Valid data is processed or sent to a service.
Template-driven vs Reactive Submission
| Feature | Template-driven | Reactive |
|---|---|---|
| Submit Event |
ngSubmit
|
ngSubmit
|
| Form Access |
NgForm
|
FormGroup
|
| Values |
form.value
|
formGroup.value
|
| Validation | Template-based | Programmatic |
| Typical Use | Simpler forms | Complex forms |
Submission Result Example
Form Submission Successful
Name: Pradnya
Course: Angular Full Stack
Form: Valid
Status: Data processed successfully
Important Points
Use the Angular ngSubmit event to handle form submission.
Check whether the form is valid before using the submitted values.
Use the form or FormGroup value to access the information entered by the user.
Keep backend communication inside a service when the application grows.
Form Submission Best Practices
- Validate form data before processing or sending it.
- Keep submission logic inside a clear component method.
- Use services for backend communication instead of placing API logic directly inside the template.
- Display a useful success or error state after submission.
- Reset the form only when the application requires it.
- Perform validation on the backend as well; client-side validation should not be the only data-integrity check.
Summary
Angular form submission collects user-entered information and processes it after the form is submitted.
Template-driven forms commonly use ngSubmit with NgForm, while reactive forms use ngSubmit with a FormGroup.
The general workflow is Input → Validation → Submission → Processing. With this, the Angular Forms module is complete. The next module is Observables.