Commit graph

87 commits

Author SHA1 Message Date
Alex Rickabaugh
24e52d450d feat(forms): add debounce option to validateAsync and validateHttp
This adds support for a `debounce` option to the `validateAsync` and `validateHttp` functions.
This allows developers to debounce the triggering of async validators to improve performance.

A `DebounceTimer` type was also added to `@angular/core` to represent the wait condition parameters uniformly.
2026-03-25 14:17:52 -07:00
Alex Rickabaugh
709f5a390c feat(forms): add FieldState.getError()
Added a `getError(kind: string)` method to `FieldState` that returns the first validation error of a given kind, or `undefined` if no such error exists. This method is reactive and will re-evaluate when errors change.

Fixes #63905

Also updated public API goldens and added unit tests.
2026-03-24 15:07:55 -07:00
Alex Rickabaugh
ee8d2098cb fix(forms): change FieldState optional properties to non-optional | undefined
This improves compatibility with TypeScript's exactOptionalPropertyTypes.

Fixes #67246
2026-03-24 14:51:31 -07:00
Leon Senft
0eeb1b5f03 fix(forms): allow FormRoot to be used without submission options (#67727)
The `[formRoot]` directive will no longer call `submit()` if the bound
form doesn't define its own submission options. This allows the
directive to be used solely for the default behavior it provides:
setting `novalidate` on the `<form>` and calling `preventDefault()` on
the `submit` event.

Fix #67367

PR Close #67727
2026-03-23 15:41:19 -07:00
Leon Senft
f4a5b42ebe refactor(forms): rename directive files for consistency (#67727)
- **FormField**:  `form_field_directive.ts` -> `form_field.ts`
- **FormRoot**:   `ng_signal_form.ts`       -> `form_root.ts`

PR Close #67727
2026-03-23 15:41:19 -07:00
Alex Rickabaugh
98c5afdb02 perf(forms): lazily instantiate signal form fields
Currently, Signal Forms eagerly instantiates all nodes in the form tree because `childrenMap` iterates over the `value` and creates a `FieldNode` for every property. This ensures validation side-effects are run early, but creates pure overhead for fields without validation logic unless explicitly accessed.

This commit makes `childrenMap` lazy by default, skipping materialization for children without schema logic. This is achieved by introducing `hasLogicRules()` and `anyChildHasLogic()` across the `LogicNode` hierarchy. Fields are now only instantiated when a direct read occurs via `getChild()` (which calls the new `ensureChildrenMap()`) or if their subtree requires eager evaluation due to existing validation rules.

Fixes #67212
2026-03-20 15:09:26 -07:00
Alex Rickabaugh
74f76d8075 feat(forms): add reloadValidation to Signal Forms to manually trigger async validation
This commit introduces a formal mechanism to manually re-trigger
asynchronous validations in Signal Forms, addressing #66994.

It exposes a `reloadValidation` method on the `FieldState` interface
that recursively cascades down the form tree and invokes the underlying
`ResourceRef`'s `reload()` method for any metadata keys tagged with the
internal `IS_ASYNC_VALIDATION_RESOURCE` symbol.

Fixes #66994
2026-03-19 15:22:13 -07:00
Alex Rickabaugh
83032e3605 fix(forms): support generic unions in signal form schemas
This commit resolves an issue where using an uninstantiated generic type
parameter in a signal form model caused TypeScript compilation failures due to
distributive conditional types (#66596). The previous attempt to fix this issue
by tuple-wrapping everything caused another bug (#65535) that prevented property
access on generic unions.

This commit balances the need to resolve nested generic property access while
handling infinitely recursive generic structures without depth errors.

What changed and why:
- Base State Wrappers: Tuple wrappers (`[TModel] extends [AbstractControl]`) are
  applied to `FieldTreeBase` to safely defer generic evaluation. This prevents
  primitive unions (like `boolean`) from incorrectly evaluating to `never`.
- Naked Map Over Children: Object subfield checks (`TModel extends Record`) are
  re-evaluated as purely naked conditionals. Eager distribution over generics
  allows users to directly access shared properties of unresolved union types.
- Array Interface Deflection: `ReadonlyArrayLike<T>` generic abstraction is
  redefined as an explicit `interface` instead of a mapped `Pick` type alias.
  This optimally intercepts TypeScript from eagerly evaluating infinitely
  recursive array structures (e.g. `RecursiveType = (number | RecursiveType)[]`).
- Overloaded Context Methods: `FieldNodeContext.stateOf` and `fieldTreeOf` are
  defined as explicitly overloaded class methods and lexically bound (`this`) in
  the constructor. These changes are required to safely align the runtime bindings
  with the tautological conditionals implemented in the `RootFieldContext`
  interface structure.

Fixes #65535
2026-03-17 17:59:36 -06:00
Kristiyan Kostadinov
b18592a1a8 build: fix failing test in forms
Fixes a test that started failing after a couple of related changes landed at the same time.
2026-03-17 15:31:35 +01:00
Leon Senft
eeba51c50b refactor(forms): make markAsTouched() touch all descendants by default
`markAsTouched()` now marks all descendants as touched. In general this
method is called when controls update the model. Most controls update
leaf nodes, in which case this change has no effect.

Marking all descendants allows triggering validation for subsections of
a form, independently from having to call `submit()` on the entire form.

`markAsTouched()` now accepts a `MarkAsTouchedOptions` parameter, which
includes a `skipDescendants` property. This can be used mark only the
receiving field as touched: `node.markAsTouched({skipDescendants: true})`.
2026-03-16 16:03:36 -06:00
kirjs
a94958b59b refactor(forms): Address more feedback
Minor test and type cleanup
2026-03-16 15:24:20 -06:00
kirjs
b061495134 refactor(forms): Add more tests
thoroughly tests both propagation directions
2026-03-16 15:24:20 -06:00
kirjs
0d92708b33 refactor(forms): adress feedback
Drop unnecessary casts + cleanup
2026-03-16 15:24:20 -06:00
kirjs
6703e6c803 refactor(forms): add structured extract filter
It's a helper function that takes a form, and extrasts it's value unwrapping compat values and also allowing to filter them.
2026-03-16 15:24:20 -06:00
Leon Senft
57ba621c81 test(forms): read only context prevents writing to field value
Test that the read only context prevents writing to a field value:

* In validation rules
* In a provided configuration
2026-03-10 15:07:42 -07:00
Leon Senft
a1a6c5282e refactor(forms): restrict reactive logic to a readonly API
Reactive logic in forms is not intended to mutate state, but this was
poorly communicated by the permissive and highly mutable field context
provided to all logic functions. This change splits all of the
state-related API into writable and readonly interfaces.

* Top-level functions that produce a `FieldTree` (e.g. `form()`) expose
  writable signals (e.g. `value: WritableSignal<T>`) and mutating
  methods (e.g. `markAsDirty()`).

* Reactive logic expose readonly signals (e.g. `value: Signal<T>`) and
  omit mutating methods.
2026-03-10 15:07:42 -07:00
Sonu Kapoor
71b8159b37 test(forms): cover transformedValue without FormField context
Adds a test verifying that `transformedValue` exposes parse errors via
the returned signal's `parseErrors()` property when no FormField
context is present.

This ensures that:
- parse errors are still observable without DI-based field propagation
- the model is not updated when `parse` omits `value`
- valid input clears parse errors and updates the model

This test protects the documented contract that DI-based error
propagation is expected for FormValueControl usage, while standalone
usage relies on explicit consumption of `parseErrors()`.
2026-03-09 16:41:48 -07:00
Leon Senft
c767d678cf
feat(forms): add 'blur' option to debounce rule
Expands the `debounce` rule configuration to accept `'blur'`. When this option
is provided, the rule will delay model synchronization until the field loses
focus (is touched). This introduces a debouncer that defers resolution
until the framework automatically aborts pending debounces upon touch events.
2026-03-05 09:55:14 -08:00
Miles Malerba
23fd8fa586 fix(forms): use consistent error format returned from parse
Aligns the errors returned from the `parse` function in
`transformedValue` to use the same convention as the rest of signal
forms (a property called `error` that can contain a single error or list
of errors)
2026-02-23 09:11:51 -08:00
Leon Senft
1a19d61e19 refactor(forms): clean up
* Remove unused `TValue` type parameter from `FormUiControl`
* Remove unused imports
* Remove unnecessary cast
2026-02-23 09:09:55 -08:00
cexbrayat
fe25c57a5c fix(forms): preserve parse errors when parse returns value
Fixes #67170 by keeping the errors even a value is returned from the parse function.
2026-02-20 10:28:54 -08:00
Miles Malerba
27397b3f4f fix(forms): clear parse errors when model updates (#66917)
Changes `parsedErrors` to a `linkedSignal` based on the model value.
This ensures that the parse errors are reset if the model changes from
outside the control.

PR Close #66917
2026-02-13 12:11:06 -08:00
Miles Malerba
ba009b6031
feat(forms): add form directive
Adds a `formRoot` directive to manage submitting the form in signal
forms.
2026-02-10 14:34:48 -08:00
Miles Malerba
f56bb07d83 feat(forms): add field param to submit action and onInvalid
The `action` and `onInvalid` handlers now recevie two pieces of
information:
1. The form that is being submitted
2. The specific field that the submit was triggered on

Remove the `submit()` method on field state - supporting this is complex
from a typing perspective, since the `FieldState` only knows its
`TValue` type, not the `TModel` type of its owning `FieldTree`. Rather
than try to pack additional generics on to `FieldState`, we'll just
leave the `submit` function as a standalone importable function.
2026-02-09 14:49:43 -08:00
Leon Senft
b772f518f1 refactor(forms): add fieldTree property to FieldState
The `fieldTree` property of `FieldState` returns its associated
`FieldTree`.

Note that the round trip from `FieldTree` to `FieldState` and back will
lose type information. This is because `FieldState` intentionally does
not know whether it came from a pure Signal Forms field tree, or a
Reactive Forms compatible field tree:

```ts
// Pure Signal Forms:
const x: FieldTree<string>;

x();           // FieldState<string>;
x().fieldTree; // FieldTree<unknown>

// Reactive Forms compatibility:
const y: FieldTree<FormControl<string>>;

y();            // FieldState<string>;
y().fieldTree;  // FieldTree<unknown>;
```
2026-02-09 12:28:35 -08:00
Miles Malerba
adfb83146b
fix(forms): simplify design of parse errors
Reoves the `parseErrors` property on `FormUiControl` and instead
introduces a new utility `transformedValue` that automatically handles
synchronizing the raw value and model value using the given `parse` and
`format` functions. It also automates the reporting of `parseErrors` to
the `FormField`, simplifying the API surface
2026-02-09 12:27:41 -08:00
Angular Robot
11767cabe4 build: update Jasmine to 6.0.0
Jasmine enables `forbidDuplicateNames: true` by default. So we also need to desambiguate duplicate spec names.
2026-02-09 12:15:57 -08:00
SkyZeroZx
24c0c5a180 feat(forms): support signal-based schemas in validateStandardSchema
Allow `validateStandardSchema()` to consume a computed schema so
validation rules stay in sync when the schema changes over time.

This supports schemas stored in computed signals (e.g. zod schemas that
depend on input signals) and ensures the effective schema updates after
initialization instead of being captured once.

Fixes #66867
2026-02-06 07:40:46 -08:00
Miles Malerba
95ecce8334 feat(forms): allow setting submit options at form-level
Updates FormOptions to accept a submission configuration object.
This allows defining default submit options (action, validation behavior, etc.)
when creating the form, which can be overridden when calling submit().
2026-02-03 12:43:31 -08:00
Miles Malerba
dd208ca259 feat(forms): update submit function to accept options object
Changes the `submit` function signature to accept a `FormSubmitOptions` object instead of a direct action callback.
This allows for more flexibility, including:

- `action`: The standard submit action to perform with the data.
- `onInvalid`: A callback to execute when the submit action is not triggered due to failing validation
- `ignoreValidators`: Controls whether pending validators or invalid validators should be ignored

Also updates the return value of `submit` to a `Promise<boolean` to indicate submission success.
2026-02-03 12:43:31 -08:00
kirjs
80f08838b0 refactor(forms): Address more feedback
Clean up tests, drop old todos
2026-02-02 14:51:40 -08:00
kirjs
fdae63c11f refactor(forms): Address more feedback
Make the way reset works for it to be more consistent
2026-02-02 14:51:40 -08:00
kirjs
e4eeb3adc0 refactor(forms): Address more feedback
Untrack callbacks, so they are not called when signals change
2026-02-02 14:51:40 -08:00
kirjs
b3b5611096 refactor(forms): Address more feedback
Document that injectors are optional
2026-02-02 14:51:40 -08:00
kirjs
0df5442f4e refactor(forms): Address more feedback
- Add more comments and docs
- In signalErrorsToValidationErrors return null for empty object
- Drop messages in prod mode
2026-02-02 14:51:40 -08:00
kirjs
8e80575ff4 refactor(forms): address feedback
Consolidate everything related to converting errors in one place
2026-02-02 14:51:40 -08:00
kirjs
05d5087252 refactor(forms): use markAsPristine and markAsUntouched on field node
This make things cleaner
2026-02-02 14:51:40 -08:00
kirjs
bbbdf0a6ed refactor(forms): add unsupported method errors and docs
- Add disable, enable methods that throw with helpful messages
- Add validator methods (set/add/remove/clear) that throw
- Add setErrors, markAsPending methods that throw
- Add setters for dirty/pristine/touched/untouched that throw
- Add JSDoc with @usageNotes examples
- Add comprehensive unit tests for SignalFormControl
- Add FormGroup/FormArray integration tests
- Add web tests for CVA directive lifecycle
- Update migration docs with SignalFormControl usage
2026-02-02 14:51:40 -08:00
Leon Senft
26d12158e1
refactor(forms): convert FieldState.controlValue to a WritableSignal
Remove `setControlValue()` from `FieldState` and convert `controlValue` to a
`WritableSignal` whose setter implements the debounced syncing behavior
of `setControlValue()`.
2026-01-30 09:14:14 -08:00
Leon Senft
e682e53113 fix(forms): only touch visible, interactive fields on submit
Don't touch hidden, disabled, or readonly fields on submit, since they
don't contribute to form validity. This also prevents errors from
appearing immediately if they're later made interactive.

Fix #66344
2026-01-28 18:56:02 +00:00
Leon Senft
01bfb83fc9 test(forms): submit behavior while validation is pending
Ensure `submit()` behaves as expected while a form is pending.

- Submission is not blocked by pending validation.
- Submission errors prevent pending validation errors from appearing
  after they resolve on the same field.
- Submission errors don't prevent pending validation errors from
  appearing after they resolve on subfields.
2026-01-28 00:15:29 +00:00
Miles Malerba
fb05fc86d0
fix(forms): sort error summary by DOM order
This will allow users to rely on the `errorSummary` order to implement
features like "focus next error"
2026-01-26 22:29:07 +00:00
Miles Malerba
ebae211add feat(forms): introduce parse errors in signal forms
Parse errors allow a custom control to communicate that it is currently
unable to produce a valid value.

Parse errors are reported by implementing the optional `parseErrors`
property on the `FormUiControl`. The property should be a signal of the
current parse errors.

Also renames several `*Field` types to `*FieldTree`. This aligns with the new naming of the concept after `Field` was renamed
to `FieldTree`.
2026-01-22 22:19:10 +00:00
Miles Malerba
e7b2dde6d1 fix(forms): fix control value syncing on touch
Previously we were unconditionally setting the control value back into
the value, regardless of if it had actually been changed. This PR
changes the logic to flush the pending sync on touch if there is one, or
just skip it if there isn't.
2026-01-16 09:25:27 -08:00
cexbrayat
7fd076bdd3 test(forms): improve test coverage for multiple pattern validators
Add comprehensive test cases to validate behavior when multiple
pattern validators are applied to the same field.

These tests clarify that multiple patterns operate with AND logic,
where each pattern is validated independently and produces its own
error when it doesn't match.
2026-01-12 13:48:16 -08:00
Miles Malerba
5671f2cc07
fix(forms): Rename signal form [field] to [formField]
This completes the rename started in #66136. `[field]` is too generic of
a selector for the forms system to own, and likely to cause naming
collisions with existing components. Therefore it is being renamed to
`[formField]`
2026-01-09 14:33:09 -08:00
Leon Senft
46dbd18566 refactor(forms): remove customError()
Remove the `customError` function and `CustomValidationError` type.

These were made obsolete by support for returning plain object literals
as custom errors.

This also catches few `field` properties that were missed in the
renaming to `fieldTree`.
2026-01-07 15:07:30 -05:00
SkyZeroZx
e7d99f02cb fix(forms): clean up abort listener after timeout
Removes the abort event listener once the debounce timeout completes.

This avoids lingering listeners, prevents potential memory leaks, and ensures
the abort logic runs at most once.
2026-01-07 14:07:25 -05:00
kirjs
282220d032 fix(forms): Support readonly arrays in signal forms
This would allow using `readonly Array<...>` in types
2026-01-02 08:26:06 +01:00
Miles Malerba
ae0c59028a
refactor(forms): rename field to fieldTree in FieldContext and ValidationError
BREAKING CHANGE:
2025-12-16 10:26:22 -08:00