How to correctly disable submit button in reactive forms in Angular

In this article, you can learn why you should always disable buttons with !form.valid.

You can view the source code for this project by following this link: GitHub

Introduction

Each form has one of three validation statuses:

  • pending: async validations are occurring and errors are not yet available for the input value
  • invalid: an error exists in the input value
  • valid: no errors exist in the input value

Writing [disable]="form.invalid", the application will not disable the button when the form is in pending status.

Solution

Disable the button with !form.valid pattern.

<button [disabled]="!form.valid">Submit</button>

Do you like the content?

Your support helps me continue my work. Please consider making a donation.

Donations are accepted through PayPal or Stripe. You do not need a account to donate. All major credit cards are accepted.

Portrait of the author

About me

On a daily basis, I work as a Front-End Developer with a primary focus on JavaScript. I feel most comfortable with Angular and NestJS, but I’m always eager to venture beyond a single tech stack. I effectively leverage AI tools in my workflow, which allows me to quickly onboard into new technologies and write code in other languages or frameworks whenever needed.

After hours, I bring my passion for tech into the physical world. I run my own homelab, where I experiment with server management and build home automation and monitoring powered by Home Assistant.

Leave a comment