> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# 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:

```typescript title="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](/guide/forms.md#numbers).
- **A form's `maxLength` sets the field's `maxlength`.** See [Constraints](/guide/forms.md#constraints).

## API

### `LYNX_FORM_ACCESSORS`

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

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

### `LynxInputValueAccessor` and `LynxTextareaValueAccessor`

```typescript
@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.

| Input       | Type                  | Description                                                                         |
| ----------- | --------------------- | ----------------------------------------------------------------------------------- |
| `type`      | `string`              | The field's `type`, which `LynxInput` takes too. `number` and `digit` give numbers. |
| `maxLength` | `number \| undefined` | The most characters the field takes, as its `maxlength`. Signal Forms sets it.      |

### `onInput()`

```typescript
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()`

```typescript
onBlur(): void;
```

Runs on `bindblur`. Marks the control as touched.

### `writeValue()`

```typescript
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](/guide/glossary.md#ui-method) instead.

### `registerOnChange()` and `registerOnTouched()`

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

Store the callbacks Angular forms passes in.

### `setDisabledState()`

```typescript
setDisabledState(isDisabled: boolean): void;
```

Sets or removes the element's `disabled` attribute.

### `validate()` and `registerOnValidatorChange()`

```typescript
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`

```typescript
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`.

## Related

- [Forms](/guide/forms.md): Signal Forms, reactive forms and `ngModel` on Lynx.
- [Elements](/api/angular-lynx/elements/index.md): `LYNX_ELEMENTS`
- [`<input>`](/api/elements/input.md) and [`<textarea>`](/api/elements/textarea.md)
