For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/forms.md.

Forms

Angular's Signal Forms, reactive forms and template-driven forms all work on Lynx. Bind Lynx's <input> and <textarea>, or the UI components, and write the same form code as on the web.

Set up

Import LYNX_ELEMENTS, which includes the value accessors for <input> and <textarea>. Then import the Angular forms API you use:

APIImport
Signal FormsFormField from @angular/forms/signals
Reactive formsReactiveFormsModule from @angular/forms
Template-driven (ngModel)FormsModule from @angular/forms

What binds

Lynx has only <input> and <textarea>: no checkbox, radio or select element. For those, use the UI components. Every control below binds to all three APIs ([formField], [formControl]/formControlName and [(ngModel)]).

ControlValueTouched when
<input>, <textarea>stringIt loses focus
<input type="number">, type="digit"number | nullIt loses focus
Input, Textarea, Search FieldstringIt loses focus
Switch, CheckboxbooleanIt's tapped or dragged
Radio GroupstringAn item is chosen
Toggle Groupstring[]An item is pressed, or its indicator dragged
SelectstringIts sheet closes
Multi-selectreadonly string[]Its sheet closes
Date PickerDate | nullIts sheet closes
CalendarDate | nullA day is picked
SlidernumberA drag or tap ends
  • Touched is when a form shows its errors. A phone's switch or select never takes the focus, so each control marks itself touched when the person is done with it.
  • A single Toggle isn't a form control. Its state is pressed, not value or checked. Use a Switch or a Checkbox in a form.

Signal Forms

src/app/sign-up.ts
import { Component, signal } from '@angular/core';
import { FormField, form, min, required, submit } from '@angular/forms/signals';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import { UiSwitch } from '../components/ui/switch';

@Component({
  selector: 'app-sign-up',
  imports: [LYNX_ELEMENTS, FormField, UiSwitch],
  template: `
    <input placeholder="Email" type="email" [formField]="signUp.email" />
    @if (signUp.email().touched() && signUp.email().errors()[0]; as error) {
      <text class="text-destructive">{{ error.message }}</text>
    }

    <input placeholder="Age" type="number" [formField]="signUp.age" />
    @if (signUp.age().touched() && signUp.age().errors()[0]; as error) {
      <text class="text-destructive">{{
        error.kind === 'parse' ? 'Enter a number' : error.message
      }}</text>
    }

    <ui-switch accessibility-label="News" [formField]="signUp.news" />

    <view class="rounded-md bg-primary px-4 py-2" (bindtap)="send()">
      <text class="text-primary-foreground">Sign up</text>
    </view>
  `,
})
export class SignUp {
  readonly #model = signal({
    email: '',
    age: null as number | null,
    news: true,
  });

  readonly signUp = form(this.#model, (s) => {
    required(s.email, { message: 'Enter your email' });
    required(s.age, { message: 'Enter your age' });
    min(s.age, 18, { message: 'You must be 18 or over' });
  });

  send(): void {
    void submit(this.signUp, {
      action: async () => console.log(this.#model()),
      // Focus the first field with an error: a text field opens the keyboard.
      onInvalid: (field) =>
        field().errorSummary()[0]?.fieldTree().focusBoundControl(),
    });
  }
}
  • submit() marks every field touched, so every error shows, and runs action only when the form is valid.
  • focusBoundControl() works, on a field or on the whole form, which focuses its first bound field. An <input>, a <textarea> and the text UI components take the focus and open the keyboard. A switch or a select has no focus to take.
  • Put the form on a <view>. Lynx has no <form> element and no submit event, so submit from a tap.

Numbers

A type="number" or type="digit" field gives its form a number, and null while it's empty, as Angular's number fields do on the web. Bind it to a number | null:

<input type="number" placeholder="Age" [formField]="profile.age" />
  • Text that isn't a number leaves the value as it was and puts a parse error on the field. That's anything but a finite decimal, as a web number field takes: 1,5, a lone -, 0x10 or Infinity.
  • The parse error has no message. Show one of your own for error.kind === 'parse', as the example above does.
  • A point with nothing after it is taken, as a browser's number field takes it typed. 1. is 1, on the way to 1.5, so typing it shows no error.
  • A field bound to a string keeps strings. A FormControl('') or a string field on a type="number" field still gets '42'. Give it a number | null to get numbers.
  • Reactive forms differ from the web here. On the web, a reactive control on a number field gets numbers whatever it started as. Here, start it as a number or null: new FormControl<number | null>(null).
  • The Input UI component holds a string. Use <input type="number"> for a number field.

Constraints

Rules in the form reach the field, so the field behaves as the form says:

RuleWhat the field does
maxLength()The keyboard stops at that many characters.
readonly()The text can't be changed.
disabled()The field and its keyboard are off.
minDate(), maxDate()A Date Picker or Calendar can't pick past them.
  • They reach <input>, <textarea>, and the UI components that have the matching input.
  • min(), max(), minLength(), pattern() and required() only validate. Lynx's fields have no attribute for them.
  • With reactive forms, bind [maxLength] on the field, as on the web: <input [formControl]="note" [maxLength]="140" />.

Reactive forms

src/app/contact.ts
import { Component } from '@angular/core';
import {
  FormControl,
  FormGroup,
  ReactiveFormsModule,
  Validators,
} from '@angular/forms';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-contact',
  imports: [LYNX_ELEMENTS, ReactiveFormsModule],
  template: `
    <view [formGroup]="form">
      <input placeholder="Your name" formControlName="name" />
      @if (form.controls.name.invalid && form.controls.name.touched) {
        <text class="text-destructive">Name is required</text>
      }
      <textarea placeholder="Message" formControlName="message"></textarea>

      <view class="rounded-md bg-primary px-4 py-2" (bindtap)="send()">
        <text class="text-primary-foreground">Send</text>
      </view>
    </view>
  `,
})
export class Contact {
  readonly form = new FormGroup({
    name: new FormControl('', {
      nonNullable: true,
      validators: Validators.required,
    }),
    message: new FormControl('', { nonNullable: true }),
  });

  send(): void {
    this.form.markAllAsTouched();
    if (this.form.valid) console.log(this.form.getRawValue());
  }
}
  • disable() and enable() work. They take the field's keyboard away and give it back.
  • Write <textarea></textarea> in full. Angular doesn't allow <textarea />.

Template-driven forms

src/app/settings.ts
import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import { UiCheckbox } from '../components/ui/checkbox';

@Component({
  selector: 'app-settings',
  imports: [LYNX_ELEMENTS, FormsModule, UiCheckbox],
  template: `
    <input placeholder="Display name" [(ngModel)]="displayName" />
    <ui-checkbox label="Show my email" [(ngModel)]="showEmail" />
  `,
})
export class Settings {
  readonly displayName = signal('');
  readonly showEmail = signal(false);
}

[(ngModel)] can bind straight to a signal.

Set a value from code

setValue(), reset() and [(ngModel)] all update what the field shows, and so does binding [value] on an <input> or <textarea>.

Don't bind [attr.value]. On iOS and Android a Lynx field has no value attribute, so it never shows. The form APIs and [value] set the field's text with its setValue method instead, and a field that renders with a value shows it from its first frame.

On-screen keyboard

src/app/search-box.ts
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-search-box',
  imports: [LYNX_ELEMENTS],
  template: `
    <input
      placeholder="Search"
      confirm-type="search"
      [class]="focused() ? 'border-2 border-ring' : 'border border-border'"
      (bindfocus)="focused.set(true)"
      (bindblur)="focused.set(false)"
      (bindconfirm)="query.set($event.detail.value)"
    />
    <text>Searching for: {{ query() }}</text>
  `,
})
export class SearchBox {
  readonly focused = signal(false);
  readonly query = signal('');
}
  • confirm-type sets the keyboard's action key. Use done (the default), next, go, search or send. Pressing it fires (bindconfirm), with the text in detail.value.
  • (bindfocus) and (bindblur) fire when the field gains and loses focus, also with detail.value.
  • Focus a field from code with focus() on its element, as on the web: this.search().nativeElement.focus(). blur() closes the keyboard.
  • type picks the keyboard: text, number, digit, password, tel or email.
  • The keyboard doesn't move the page. Lynx's <input> doesn't scroll out of the keyboard's way, so the keyboard can cover the field being typed in. Put long forms in a <scroll-view> with extra space at the bottom, so people can scroll a covered field into view.
  • A field at the bottom of the screen can rise above the keyboard itself, as the Search Field does. The input's (bindkeyboardheightchange) sends the keyboard's height in detail.height: points on iOS, physical pixels on Android.
  • avoid-keyboard moves the whole page. It's Lynx's own keyboard avoidance on an <input>: avoid-keyboard="true", with avoid-keyboard-spacing="8px". It shifts the entire Lynx view up until the focused field clears the keyboard, so the top of the page goes off screen. It suits a form. A bar at the bottom should rise on its own instead.

Limitations

  • No <label for>. Lynx doesn't link labels to fields. Put a <text> next to the field, or use the Label component.
  • No :invalid or :focus styles. Lynx has no form pseudo-classes. Style from the field's state instead, such as [class.border-destructive]="field().invalid()".