Skip to content

Latest commit

 

History

History
120 lines (81 loc) · 3.53 KB

Forms.md

File metadata and controls

120 lines (81 loc) · 3.53 KB

Forms in Angular

Angular provides robust solutions to handle user input through forms using two approaches: template-driven forms and reactive forms. This document explores these approaches and discusses how to implement form validation, ensuring data integrity and user feedback.

Table of Contents


Template-driven Forms

Template-driven forms are suitable for small or simple forms, leveraging the power of Angular directives to create forms using a minimal amount of JavaScript code.

How to Implement Template-driven Forms

To create a template-driven form:

  1. Create the Form in the HTML Template:
<form #myForm="ngForm">
  <input type="text" name="firstName" ngModel required>
  <button type="submit">Submit</button>
</form>
  1. Access Form Data in the Component:
import { NgForm } from '@angular/forms';

export class AppComponent {
  onSubmit(form: NgForm) {
    console.log(form.value);
  }
}

Here, ngModel is used to bind the input field to a model property, and the form submission is handled by the onSubmit method.

Reactive Forms

Reactive forms offer more flexibility, scalability, and reusability by handling form input with reactive patterns. This approach is more robust and allows better control over form behaviors.

How to Implement Reactive Forms

To create a reactive form:

  1. Define the Form Model in the Component Class:
import { FormGroup, FormControl } from '@angular/forms';

export class AppComponent {
  myForm = new FormGroup({
    firstName: new FormControl('')
  });
}
  1. Link the Form Model to the Form in the Template:
<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
  <input type="text" formControlName="firstName">
  <button type="submit">Submit</button>
</form>

Listening for Changes

Reactive forms also allow you to listen for changes in form values, providing a powerful way to react to user input dynamically.

this.myForm.valueChanges.subscribe(changes => {
  console.log(changes);
});

Form Validation

Both form approaches support validation. Angular provides built-in validators, but you can also create custom validators if needed.

Built-in Validators

Common built-in validators include required, minLength, maxLength, and pattern. These can be applied directly in the template for template-driven forms or in the component class for reactive forms.

Custom Validators

Custom validators can be defined as functions that receive a FormControl and return a map of validation errors or null if no errors are found.

function customValidator(control: FormControl): { [key: string]: any } | null {
  const isValid = /* Your validation logic */;
  return isValid ? null : { 'customError': true };
}

Implementing Validation in Reactive Forms

this.myForm = new FormGroup({
  firstName: new FormControl('', [customValidator, Validators.required])
});

Displaying Validation Errors in the Template

<p *ngIf="myForm.get('firstName').errors?.required">First name is required.</p>
<p *ngIf="myForm.get('firstName').errors?.customError">Custom error message.</p>

Understanding and utilizing Angular forms and validation effectively can dramatically enhance the interactivity and user experience of your web applications. These features allow you to capture user input reliably and securely in both simple and complex forms.