Signal Forms Architecture
The packages/forms/signals directory contains an experimental, signal-based forms API for Angular.
This system differs significantly from the existing Reactive and Template-driven forms.
Mental Model
Model-Driven: The system is built around a WritableSignal<T> which serves as the single source of truth.
Unlike Reactive Forms where the FormControl holds the value, here the Signal holds the value.
The form is merely a view or projection of that signal, adding form-specific state (validity, dirty, touched).
Proxy-Based Traversal: The form API (form(signal)) returns a FieldTree. This object is a Proxy.
It allows accessing nested fields (e.g., myForm.user.name) without manually creating control groups.
Accessing a property on the proxy lazily resolves or creates the corresponding FieldNode.
Schema-Based Logic: Validation, disabled state, and other metadata are defined separately via Schemas.
Schemas are applied to the form structure using functions like apply, applyEach (for arrays), and applyWhen.
This separates the structure of the data from the rules governing it.
Directives as Glue: The [formField] directive binds a DOM element (native input or custom control) to a FieldNode.
It handles:
- Syncing the value between the DOM and the Signal.
- Reflecting state (valid, touched, etc.) to the UI.
- Handling user interaction events (blur, input).
Key Components
1. FieldNode (src/field/node.ts)
The central internal class representing a single field in the form graph. It aggregates several state managers:
structure: Manages parent/child relationships and signal slicing.
validationState: Computes valid, invalid, errors signals.
nodeState: Tracks touched, dirty, pristine.
metadataState: Stores metadata like min, max, required.
submitState: Tracks submission status and server errors.
2. ValidationState (src/field/validation.ts)
Manages the complexity of validation:
- Synchronous Errors: Derived from schema rules.
- Asynchronous Errors: Handled via signals, including 'pending' states.
- Tree Errors: Errors that bubble up or are targeted at specific fields.
- Submission Errors: Server-side errors injected imperatively via
submit().
3. FormField Directive (src/directive/form_field_directive.ts)
The bridge between the FieldNode and the DOM.
- Selector:
[formField]
- It supports:
- Native Elements:
<input>, <select>, <textarea>.
- Custom Controls: Components implementing
FormUiControl or FormValueControl.
- Legacy Interop: Components implementing
ControlValueAccessor (via InteropNgControl).
4. Schema (src/api/structure.ts & src/api/rules)
Defines the behavior.
- Created via
schema(fn).
- Applied via
apply(path, schema).
- Rules include validators (
required, pattern, min, max) and state modifiers (disabled, hidden).
Data Flow
- Read:
form.field.value() reads directly from the underlying signal (projected to the specific path).
- Write: Writing to the form (e.g., via UI) updates the underlying signal.
- Validation: A computed effect observes the value signal and runs validators defined in the schema.
Usage Example (Conceptual)
// 1. Define Model
const user = signal({name: '', age: 0});
// 2. Define Schema
const userRules = schema((u) => {
required(u.name);
min(u.age, 18);
});
// 3. Create Form
const userForm = form(user, userRules); // OR apply(userForm, userRules)
// 4. Bind in Template
// <input [formField]="userForm.name">
Important Files
packages/forms/signals/src/api/structure.ts: Public API entry points (form, apply).
packages/forms/signals/src/api/control.ts: Interfaces for custom controls (FormUiControl).
packages/forms/signals/src/field/node.ts: The FieldNode implementation.
packages/forms/signals/src/directive/form_field_directive.ts: The [formField] directive.
Supplemental Information
- Compiler & Core Integration: Details how
[formField] hooks into type-checking and the runtime.
1---2name: reference-signal-forms3description: Explains the mental model and architecture of the code under `packages/forms/signals`. You MUST use this skill any time you plan to work with code in `packages/forms/signals`4---56# Signal Forms Architecture78The `packages/forms/signals` directory contains an experimental, signal-based forms API for Angular.9This system differs significantly from the existing Reactive and Template-driven forms.1011## Mental Model12131. **Model-Driven**: The system is built around a `WritableSignal<T>` which serves as the **single source of truth**.14 Unlike Reactive Forms where the `FormControl` holds the value, here the `Signal` holds the value.15 The form is merely a _view_ or _projection_ of that signal, adding form-specific state (validity, dirty, touched).16172. **Proxy-Based Traversal**: The form API (`form(signal)`) returns a `FieldTree`. This object is a **Proxy**.18 It allows accessing nested fields (e.g., `myForm.user.name`) without manually creating control groups.19 Accessing a property on the proxy lazily resolves or creates the corresponding `FieldNode`.20213. **Schema-Based Logic**: Validation, disabled state, and other metadata are defined separately via **Schemas**.22 Schemas are applied to the form structure using functions like `apply`, `applyEach` (for arrays), and `applyWhen`.23 This separates the _structure_ of the data from the _rules_ governing it.24254. **Directives as Glue**: The `[formField]` directive binds a DOM element (native input or custom control) to a `FieldNode`.26 It handles:27 - Syncing the value between the DOM and the Signal.28 - Reflecting state (valid, touched, etc.) to the UI.29 - Handling user interaction events (blur, input).3031## Key Components3233### 1. `FieldNode` (`src/field/node.ts`)3435The central internal class representing a single field in the form graph. It aggregates several state managers:3637- `structure`: Manages parent/child relationships and signal slicing.38- `validationState`: Computes `valid`, `invalid`, `errors` signals.39- `nodeState`: Tracks `touched`, `dirty`, `pristine`.40- `metadataState`: Stores metadata like `min`, `max`, `required`.41- `submitState`: Tracks submission status and server errors.4243### 2. `ValidationState` (`src/field/validation.ts`)4445Manages the complexity of validation:4647- **Synchronous Errors**: Derived from schema rules.48- **Asynchronous Errors**: Handled via signals, including 'pending' states.49- **Tree Errors**: Errors that bubble up or are targeted at specific fields.50- **Submission Errors**: Server-side errors injected imperatively via `submit()`.5152### 3. `FormField` Directive (`src/directive/form_field_directive.ts`)5354The bridge between the `FieldNode` and the DOM.5556- Selector: `[formField]`57- It supports:58 - **Native Elements**: `<input>`, `<select>`, `<textarea>`.59 - **Custom Controls**: Components implementing `FormUiControl` or `FormValueControl`.60 - **Legacy Interop**: Components implementing `ControlValueAccessor` (via `InteropNgControl`).6162### 4. `Schema` (`src/api/structure.ts` & `src/api/rules`)6364Defines the behavior.6566- Created via `schema(fn)`.67- Applied via `apply(path, schema)`.68- Rules include validators (`required`, `pattern`, `min`, `max`) and state modifiers (`disabled`, `hidden`).6970## Data Flow71721. **Read**: `form.field.value()` reads directly from the underlying signal (projected to the specific path).732. **Write**: Writing to the form (e.g., via UI) updates the underlying signal.743. **Validation**: A computed effect observes the value signal and runs validators defined in the schema.7576## Usage Example (Conceptual)7778```typescript79// 1. Define Model80const user = signal({name: '', age: 0});8182// 2. Define Schema83const userRules = schema((u) => {84 required(u.name);85 min(u.age, 18);86});8788// 3. Create Form89const userForm = form(user, userRules); // OR apply(userForm, userRules)9091// 4. Bind in Template92// <input [formField]="userForm.name">93```9495## Important Files9697- `packages/forms/signals/src/api/structure.ts`: Public API entry points (`form`, `apply`).98- `packages/forms/signals/src/api/control.ts`: Interfaces for custom controls (`FormUiControl`).99- `packages/forms/signals/src/field/node.ts`: The `FieldNode` implementation.100- `packages/forms/signals/src/directive/form_field_directive.ts`: The `[formField]` directive.101102## Supplemental Information103104- [Compiler & Core Integration](references/integration.md): Details how `[formField]` hooks into type-checking and the runtime.