Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Angular Reactive Forms-Login Form Made Simple
vetriselvan Panneerselvam
vetriselvan Panneerselvam

Posted on • Edited on

Angular Reactive Forms-Login Form Made Simple

Hey Devs,

As frontend developers, we often need to create forms. In fact, building a form is usually one of the first things we do when learning a new framework. One of the most common forms is the login form. In Angular, there are two main ways to handle forms:

  • Template-Driven Forms
  • Reactive Forms

Template-Driven Forms are the older approach, where validation is handled in the template. We’ll explore this method in a separate blog post.

Reactive Forms, on the other hand, are the preferred way to handle forms in Angular. They allow you to:

  • Handle validation in the component
  • Easily build complex forms
  • Test, reuse, and maintain your code more effectively

Let me show you how to create a login form in Angular using Reactive Forms.

A form consists of a collection of FormControls—these are basically the input fields. The form itself is bound to a FormGroup, which contains these FormControls.

Let’s start with a simple login form example.

Our login form will have two input fields:

  • Username
  • Password

And two buttons:

  • Login
  • Reset

We’ll create a FormGroup with two FormControls: username and password. The easiest way to do this is by using FormBuilder, a service that helps you create FormGroups.

You can inject FormBuilder into your component like this:

formGroup:FormGroup=this.formBuilder.nonNullable.group({username:['',[Validators.required]],password:['',[Validators.required]],});
Enter fullscreen modeExit fullscreen mode

The nonNullable property ensures the FormGroup doesn’t accept null values.

  • username is a FormControl for storing the username.
  • password is a FormControl for storing the password.
  • Validators.required ensures both fields are filled in.

In your HTML, the form might look like this:

<form[formGroup]="loginForm"(ngSubmit)="submit()"class="form"><divclass="form-group"><labelfor="username">Username</label><inputtype="text"formControlName="username"placeholder="Enter your username"></div><divclass="form-group"><labelfor="password">Password</label><inputtype="password"formControlName="password"placeholder="Enter your password"></div><buttonclass="form-submit-btn"type="reset">Reset</button><buttonclass="form-submit-btn"type="submit">Submit</button></form>
Enter fullscreen modeExit fullscreen mode

Now, let’s handle validation messages in the HTML.

To show an error message when the username is required:

<span*ngIf="loginForm.get('username')?.touched && loginForm.get('username')?.errors?.['required']"class="error-message">  Username is required</span>
Enter fullscreen modeExit fullscreen mode

This way, the error message only appears after the user has interacted with the field (i.e., when it’s touched). You should handle the password field in the same way.

For form submission, your component might have:

submit(){console.log(this.loginForm.value);// Handle form submission here}
Enter fullscreen modeExit fullscreen mode

Let’s talk about the reset button. By setting the button’s type to "reset", the form will reset automatically when clicked. Alternatively, you can reset the form programmatically:

reset(){this.loginForm.reset();}
Enter fullscreen modeExit fullscreen mode

Now, let’s discuss the status of a FormGroup.

A FormGroup has a status property, which can be one of the following:

  • PENDING
  • VALID
  • INVALID
  • DIRTY
  • TOUCHED
  • UNTOUCHED
  • PRISTINE

To check the status of the form:

 @if(loginForm.get('username')?.touched&& loginForm.get('username')?.errors?.['required']) {<spanclass="error-message">Username is required</span>    }
Enter fullscreen modeExit fullscreen mode

Or, you can use these boolean properties:

  • loginForm.valid: true if the form is valid
  • loginForm.invalid: true if the form is invalid
  • loginForm.dirty: true if the form has been modified
  • loginForm.pristine: true if the form is unchanged
  • loginForm.touched: true if any field has been touched
  • loginForm.untouched: true if no field has been touched

📁 Want the Full Code?
If you'd like to reference the complete code used in this blog, you can find it at the bottom of the post. It includes everything from the component setup to the HTML template and validation logic — perfect for copy-pasting or experimenting in your own project.

Source

Conclusion:

I hope this helps you understand the basics of Reactive Forms in Angular. In the next blog, we’ll dive deeper into events, validation, and custom validators.

💬Got questions or use cases you want to share? Drop a comment below! Let's discuss more Angular magic. ✨

✍️ Author:Vetriselvan

👨‍💻 Frontend Developer | 💡 Code Enthusiast | 📚 Lifelong Learner | ✍️ Tech Blogger | 🌍 Freelance Developer

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

👋 Hi, I'm Vetriselvan. A passionate front-end developer who loves turning ideas into clean, functional, and user-friendly interfaces. I enjoy writing code and sharing what I learn along the way.
  • Work
    Team lead at Intellect Design Arena
  • Joined

More fromvetriselvan Panneerselvam

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp