Angular + Reactive Forms - Required Checkbox Example

Angular + Reactive Forms - Required Checkbox Example

This is a quick example of how to implement a required checkbox field in Angular with Reactive Forms.

This is a quick example of how to implement a required checkbox field in Angular with Reactive Forms. For a more detailed registration form example that includes a bunch of other fields see Angular 10 - Reactive Forms Validation Example.

Here it is in action:

 (See on StackBlitz at https://stackblitz.com/edit/angular-reactive-forms-required-checkbox)

Angular Required Checkbox App Component

The app component contains an example form (FormGroup) which contains a single boolean field with a requiredTrue validator to make a checkbox field required. The app component uses a FormBuilder to create an instance of a FormGroup that is stored in the form property. The form property is then bound to the <form> element of the app template below using the [formGroup] directive.

The component contains a convenience getter property f to make it easier to access form controls from the template. So for example you can access the confirmPassword field in the template using f.confirmPassword instead of registerForm.controls.confirmPassword.

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({ selector: 'app', templateUrl: 'app.component.html' })
export class AppComponent implements OnInit {
    form: FormGroup;
    submitted = false;

    constructor(private formBuilder: FormBuilder) { }

    ngOnInit() {
        this.form = this.formBuilder.group({
            acceptTerms: [false, Validators.requiredTrue]
        });
    }

    // convenience getter for easy access to form fields
    get f() { return this.form.controls; }

    onSubmit() {
        this.submitted = true;

        // stop here if form is invalid
        if (this.form.invalid) {
            return;
        }

        // display form values on success
        alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.form.value, null, 4));
    }

    onReset() {
        this.submitted = false;
        this.form.reset();
    }
}

App Component Template

The app component template contains the html markup for displaying the example required checkbox form in the browser. The form element uses the [formGroup] directive to bind to the form FormGroup in the app component above.

The form binds the form submit event to the onSubmit() handler in the app component using the Angular event binding (ngSubmit)="onSubmit()". Validation messages are displayed only after the user attempts to submit the form for the first time, this is controlled with the submitted property of the app component.

The reset button click event is bound to the onReset() handler in the app component using the Angular event binding (click)="onReset()".

angular reactive forms required checkbox component formgroup

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Angular 11 Reactive Forms Validation Tutorial

Reactive form validation in angular 11 app. In this tutorial, you will learn how to use reactive form validation in angular 11 app.

Angular 8 Forms | Angular Reactive and Template Forms

We will see mode-driven approach and template-driven approach in this angular forms. Angualr forms are very useful to get the feedback from the clients.

Reactive Forms in Angular 10 - How to Create a Login Form using Reactive Forms

Learn the basics of reactive forms in Angular 10. In this tutorial, you'll look at how to Create a Login Form using Reactive Forms

Angular Reactive Forms: Reactive Forms in Angular 9 Example

Angular Reactive Forms or Model-Driven is another way to construct forms in Angular. In Angular Reactive Forms, the form isĀ built in the component class.

Angular 10 - Reactive Forms Validation Example

How to implement validation in Angular 10 using Reactive Forms. Includes an example registration form that validates on submit, a custom validator for matching password & confirm password fields, date validation and a required checkbox.