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 /api/angular-lynx/elements/form-accessors.md.

Form Accessors

The directives that connect <input> and <textarea> to Angular forms: Signal Forms' [formField], reactive forms and ngModel. import { LYNX_FORM_ACCESSORS, LynxInputValueAccessor, LynxTextareaValueAccessor, type LynxFieldValue, type LynxFieldParseError } from '@blotch/angular-lynx';

Usage

LYNX_ELEMENTS already contains both accessors. Bind a form directive, and the accessor switches on:

src/app/profile.ts
import { Component, signal } from '@angular/core';
import { FormField, form } from '@angular/forms/signals';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-profile',
  imports: [LYNX_ELEMENTS, FormField],
  template: `
    <input placeholder="Name" [formField]="profile.name" />
    <input placeholder="Age" type="number" [formField]="profile.age" />
    <textarea placeholder="Bio" [formField]="profile.bio"></textarea>
  `,
})
export class Profile {
  readonly #model = signal({ name: '', age: null as number | null, bio: '' });
  readonly profile = form(this.#model);
}
  • Angular forms calls their members. You don't.
  • A type="number" or type="digit" field gives a number, or null while it's empty, unless the form gave it a string.
  • Text that isn't a number leaves the value as it was and puts a parse error on the field. See Numbers.
  • A form's maxLength sets the field's maxlength. See Constraints.

API

LYNX_FORM_ACCESSORS

const LYNX_FORM_ACCESSORS: readonly [
  typeof LynxInputValueAccessor,
  typeof LynxTextareaValueAccessor,
];

Both form value accessors as one array. LYNX_ELEMENTS already contains them.

LynxInputValueAccessor and LynxTextareaValueAccessor

@Directive({
  selector:
    'input[formControlName], input[formControl], input[ngModel], input[formField]',
})
class LynxInputValueAccessor extends LynxFieldValueAccessor {}

@Directive({
  selector:
    'textarea[formControlName], textarea[formControl], textarea[ngModel], textarea[formField]',
})
class LynxTextareaValueAccessor extends LynxFieldValueAccessor {}

class LynxFieldValueAccessor implements ControlValueAccessor, Validator {}

Connect <input> and <textarea> to Signal Forms' [formField], reactive forms and ngModel.

  • They switch on only when one of those form directives is on the element.
  • Both have the members of LynxFieldValueAccessor, below.
InputTypeDescription
typestringThe field's type, which LynxInput takes too. number and digit give numbers.
maxLengthnumber | undefinedThe most characters the field takes, as its maxlength. Signal Forms sets it.

onInput()

onInput(event: LynxEvent<InputDetail>): void;

Runs on bindinput. Sends the typed text, event.detail.value, to the form control: as a number in a number field.

onBlur()

onBlur(): void;

Runs on bindblur. Marks the control as touched.

writeValue()

writeValue(value: unknown): void;

Shows the form value in the field, as a [value] binding does: from the field's first render, and after form.reset() and setValue().

  • null and NaN show as an empty field.
  • On iOS and Android a Lynx field has no value attribute. So the element sets its text with the setValue UI method instead.

registerOnChange() and registerOnTouched()

registerOnChange(fn: (value: LynxFieldValue) => void): void;
registerOnTouched(fn: () => void): void;

Store the callbacks Angular forms passes in.

setDisabledState()

setDisabledState(isDisabled: boolean): void;

Sets or removes the element's disabled attribute.

validate() and registerOnValidatorChange()

validate(): ValidationErrors | null;
registerOnValidatorChange(fn: () => void): void;

validate() returns { parse: { text } } while a number field's text isn't a number, and null otherwise.

  • Signal Forms reads it as the field's parse error.
  • Reactive and template-driven forms read it as one more validator.

Types

LynxFieldValue and LynxFieldParseError

type LynxFieldValue = string | number | null;
type LynxFieldParseError = { readonly text: string };

What a field gives its form, and the context of its parse error.

ControlValueAccessor, Validator and ValidationErrors come from @angular/forms.