Validation on Angular 7 form works correctly with Bootstrap checkbox when it is checked, however, it fails to invalidate when left unchecked

I am encountering an issue with a registration form that includes a checkbox for terms of service. The 'Register' button on the form is supposed to become enabled when the form is valid, and disabled when it is not. However, I noticed that even if I fill out the form correctly and check the checkbox, the register button stays enabled. This means that the form remains in a valid state even when I uncheck the checkbox.

Just as a side note, I tried adding some statements to the input fields but unfortunately, it did not solve the problem.

Below you can find my form along with a snippet of the .ts code. Some parts of the code have been omitted for brevity.

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit, AfterViewInit {
  // Some variables are removed here
  registerForm: FormGroup;
  bsConfig: Partial<BsDatepickerConfig>;
  terms = false;
  
  

  constructor(private authService: AuthService, private router: Router,
              private alertService: AlertService, private fb: FormBuilder, private zone: NgZone) {}

  ngOnInit() {
    this.bsConfig = {
      containerClass: 'theme-red'
    };
    this.createRegisterForm();
  }
  
  createRegisterForm() {
    this.registerForm = this.fb.group({
      terms: ['', Validators.required],
    });
  }
<form [formGroup]="registerForm" (ngSubmit)="register()">
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input" id="gridCheck" 
         [ngClass]="{'is-invalid': registerForm.get('terms').errors && registerForm.get('terms').touched}" 
         name="terms" 
         type="checkbox"  
         formControlName="terms">
      <label class="form-check-label" for="gridCheck">Agree to terms</label>
    </div>
  </div>

  <div class="form-group text-center">
    <button class="btn btn-yb w-100" [disabled]="!registerForm.valid" type="submit">
        <span *ngIf="loadingRegister" class="spinner-border spinner-border-sm color-yb-white" role="status" aria-hidden="true"></span>
        <span class="sr-only">Loading...</span>
        <span *ngIf="!loadingRegister">Register</span>
    </button>
  </div>
</form>

Answer №1

Oh, I just noticed something:

To ensure the checkbox control works correctly, make sure to utilize Validators.requiredTrue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a problem with the Material UI Autocomplete component when trying to implement

I am trying to integrate a Material UI autocomplete component with a checkbox component. Is there a way to have the checkbox get checked only when a user selects an option from the autocomplete? You can check out my component through the following links: ...

Discovering ways to align specific attributes of objects or target specific components within arrays

I am trying to compare objects with specific properties or arrays with certain elements using the following code snippet: However, I encountered a compilation error. Can anyone help me troubleshoot this issue? type Pos = [number, number] type STAR = &quo ...

Shifting successive elements in an array alternates

I have been working on a pick list component and have come up with the following layout https://i.stack.imgur.com/TnHAp.gif Whenever I try to move consecutive items, it keeps toggling between those two Here's a snippet of my code: moveDown(){ ...

Union indicating faults while destructuring within component

I am encountering an issue with discriminating unions in TypeScript that appears as follows: interface WithAction { isActionBtnRequired: true; content: string; containerClass?: string; customActionComponent?: ReactNode; actionComponentPosition?: ...

Cross-origin resource sharing (CORS) implemented in an environment combining Electron,

I'm currently working on a desktop application using Electron with Angular for the front end and node.js (express server) for the backend API. The node.js express server is running at http://localhost:3000, while the Angular app wrapped inside electr ...

Tips for sending just the updated section of the form

I am working with a form group where I map the values from the form to an object type in order to make a request to edit the item. This is my form structure: public companyForm = new FormGroup( { generalInfo: new FormGroup({ name: new ...

Oops! Looks like Deno encountered an error (TS2339) because it couldn't find the property x

Exploring Oak and Deno for the first time has been an exciting journey. I came across a helpful guide at along with the official documentation. My aim is to use REST-API deno by utilizing the script provided in (server.ts). import { v4 } from 'https: ...

Adding pixels to the top position with jQuery in Angular 2

I am trying to adjust the position of my markers when the zoom level is at 18 or higher by adding 10px upwards. However, my current approach doesn't seem to be working as expected. Can someone please assist me with this issue? You can view the code sn ...

TypeScript test framework for testing API calls in VS Code extensions

My VS Code extension in TypeScript uses the Axios library for API calls. I have written tests using the Mocha framework, which are run locally and through Github Actions. Recently, I integrated code coverage reporting with `c8` and I am looking to enhanc ...

Issue: ChildrenOutletContexts provider not found

I am encountering difficulties using angular/material with Angular 5. Despite following a basic Tutorial, the webpage goes blank whenever I include any material tags in the component HTML. When running ng serve with Angular CLI, no issues are reported. D ...

Ways to address conflicts with npm peer dependencies

I am in the process of upgrading my Angular version from 11 to 14. Almost all of the peer dependencies have been resolved successfully, except for one remaining issue. npm ERR! Found: @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

I encountered an error when trying to install '@angular/cli' using the command 'sudo npm install -g @angular/cli' on Ubuntu

Encountering a problem while attempting to install Angular-CLI on Ubuntu My current Node.js and npm versions are as follows: Node-v9.11.1 npm - 5.6.0 The command I ran was: savera9@savera9-desktop:~$ sudo npm install -g @angular/cli Howev ...

Error Encountered with vscode.workspace.workspaceFolders. Troubleshooting VS Code Extensions

While developing an extension for vscode, I encountered an error that I'm having trouble resolving. The goal is to create a script that generates a new file based on user inputs, but I'm running into an issue when trying to retrieve the path for ...

Issue with the deprecated router package in Angular 2 rc3

Welcome Angular 2 rc.3! After following the instructions in the 5 Min Quickstart and using project.json to configure my project, I encountered an error when running npm install: No compatible version found: @angular/<a href="/cdn-cgi/l/email-protectio ...

AngularFire UPDATE -> APPLY CHANGES

I can't seem to figure this out. I'm wondering how to UPDATE a document that is returned in the WHERE clause using AngularFire: constructor(private db: AngularFirestore) { } var path = this.db.collection('users').doc('type') ...

What are the Typescript definitions for managing events and handlers for checkboxes in React?

I am currently working on a project similar to this React example but in Typescript. The main objective is to create a parent component with state, and have multiple stateless child components that communicate back their interactions to the parent. Since ...

In Angular 15, CSS override configurations do not function as intended

Exploring the world of Angular is exciting, and I am a newcomer to it. Currently, I am tackling an innovative Angular 15 project that makes use of the Material library. My current predicament lies in the fact that none of the CSS overrides appear to be tak ...

The global and centered positioning in @angular/cdk/overlay is not functioning as expected

I am currently experimenting with the new @angular/cdk library, but I am having trouble getting the position strategy to work. I simply want to display a modal that is centered on the screen with a backdrop. I know I can apply a class to the pane and use f ...

The CORS policy is blocking my API access request from origin

I have encountered an issue while calling my API method from the client app. My technology stack includes .NET Core v2.1.6 and .NET SDK v2.1.502 on the server side, and Angular v5.2.0 on the client side. The error message states: "Access to XMLHttpReques ...

Unlocking Security in Angular 2

I am struggling with the issue of security in Angular 2. I am attempting to calculate the width of a span element within an ngfor loop: <span style="width:updateStyle({{ ((date | amDifference : item.startdate : 'minutes' :true)/item.duratio ...