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

A JavaScript specialist with many years of industry experience whose heart beats for Angular. A follower of the "Keep it simple, stupid" principle and a fan of clean code and good architecture. Fearless in the face of the toughest challenges, I always look for simple and effective solutions. As a pragmatist and an enthusiast of new technologies, I passionately follow the trends in the JavaScript world. After hours — a sailor who loves discovering new places and spending time on trips. My favorite motto? "Talk is cheap. Show me the code."

Leave a comment