# Overview

TanStack Form is the ultimate solution for handling forms in web applications, providing a powerful and flexible approach to form management. Designed with first-class TypeScript support, headless UI components, and a framework-agnostic design, it streamlines form handling and ensures a seamless experience across various front-end frameworks.

## Motivation

Most web frameworks do not offer a comprehensive solution for form handling, leaving developers to create their own custom implementations or rely on less-capable libraries. This often results in a lack of consistency, poor performance, and increased development time. TanStack Form aims to address these challenges by providing an all-in-one solution for managing forms that is both powerful and easy to use.

With TanStack Form, developers can tackle common form-related challenges such as:

- Reactive data binding and state management
- Complex validation and error handling
- Accessibility and responsive design
- Internationalization and localization
- Cross-platform compatibility and custom styling

By providing a complete solution for these challenges, TanStack Form empowers developers to build robust and user-friendly forms with ease.

## Enough talk, show me some code already!

In the example below, you can see TanStack Form in action with the Angular framework adapter:

<!-- ::start:tabs variant="files" -->

```ts ignore file="App.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core'
import {
  TanStackField,
  injectForm,
  injectSelector,
} from '@tanstack/angular-form'
import { FieldInfoComponent } from './FieldInfo'
import { TextInputComponent } from './TextInput'

@Component({
  selector: 'app-root',
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [TanStackField, TextInputComponent, FieldInfoComponent],
  template: `
    <form (submit)="handleSubmit($event)">
      <!-- A type-safe field directive -->
      <div
        [tanstackField]="form"
        name="firstName"
        [validators]="firstNameValidators"
        #firstName="field"
      >
        <!-- Use your own markup and UI libraries! -->
        <label [for]="firstName.api.name">First name</label>
        <app-text-input [field]="firstName.api" />
        <app-field-info [field]="firstName.api" />
      </div>

      <div [tanstackField]="form" name="lastName" #lastName="field">
        <label [for]="lastName.api.name">Last name</label>
        <app-text-input [field]="lastName.api" />
        <app-field-info [field]="lastName.api" />
      </div>

      <!-- Read selected form state as Angular signals. -->
      <button type="submit" [disabled]="!canSubmit()">
        {{ isSubmitting() ? 'Submitting...' : 'Submit' }}
      </button>
      <!-- Restore default values and clear validation and interaction state. -->
      <button type="reset" (click)="handleReset($event)">Reset</button>
    </form>
  `,
})
export class AppComponent {
  form = injectForm({
    defaultValues: {
      firstName: '',
      lastName: '',
    },
    onSubmit: async ({ value }) => {
      // Do something with the validated form values
      console.log(value)
    },
  })

  firstNameValidators = [
    {
      // Validate on change as well as on submit.
      triggers: ['change'] as const,
      run: ({ value }: { value: string }) => {
        if (value.trim().length === 0) {
          return 'Enter your first name'
        }
        return undefined
      },
    },
  ]

  canSubmit = injectSelector(this.form, (state) => state.canSubmit)
  isSubmitting = injectSelector(this.form, (state) => state.isSubmitting)

  handleSubmit(event: Event) {
    event.preventDefault()
    // Validate the form before calling onSubmit.
    void this.form.handleSubmit().catch(console.error)
  }

  handleReset(event: Event) {
    event.preventDefault()
    this.form.reset()
  }
}
```

```ts ignore file="TextInput.ts"
import { ChangeDetectionStrategy, Component, input } from '@angular/core'
import type { FieldWithValue } from '@tanstack/angular-form'

// This file provides the UI. Use any UI library you prefer.
@Component({
  selector: 'app-text-input',
  standalone: true,
  changeDetection: ChangeDetectionStrategy.Default,
  template: `
    <input
      #inputElement
      [id]="field().name"
      [name]="field().name"
      [value]="field().value"
      (input)="field().handleChange(inputElement.value)"
      (blur)="field().handleBlur()"
    />
  `,
})
export class TextInputComponent {
  field = input.required<FieldWithValue<string>>()
}
```

```ts ignore file="FieldInfo.ts"
import { ChangeDetectionStrategy, Component, input } from '@angular/core'
import type { AnyFieldApi } from '@tanstack/angular-form'

// This file provides the UI. Use any UI library you prefer.
@Component({
  selector: 'app-field-info',
  standalone: true,
  changeDetection: ChangeDetectionStrategy.Default,
  template: `
    @if (!field().meta.isValid) {
      <p role="alert">{{ errorMessages }}</p>
    }
  `,
})
export class FieldInfoComponent {
  field = input.required<AnyFieldApi>()

  get errorMessages() {
    return this.field()
      .errors.map((error) => error.message)
      .join(', ')
  }
}
```

<!-- ::end:tabs -->

## Next steps

Use [Installation](./installation) to set up your framework package. The
[Form API](./reference/interfaces/FormApi) and
[Field API](./reference/interfaces/FieldApi) references describe the methods and
state available as you extend your form.
