Commit graph

680 commits

Author SHA1 Message Date
kirjs
e3f5f34732 refactor(forms): Make reset take value
Now you can do form.reset({name: 'cat', age: 4});

(cherry picked from commit dec222d4d7)
2025-11-25 10:51:38 -05:00
Miles Malerba
7d5c7cf99a feat(forms): add DI option for classes on Field directive
Adds a DI configuration option for signal forms that allows the
developer to specify CSS classes that should be automatically added
by the `Field` directive based on the field's status.

(cherry picked from commit c70e246c23)
2025-11-25 10:33:41 -05:00
Leon Senft
8acf5d2756 fix(forms): allow dynamic type bindings on signal form controls
The type checker will no longer prohibit binding the Signal Forms `[field]`
directive to an input with a dynamic `[attr.type]` or `[type]` binding.

(cherry picked from commit 3a1eb07c46)
2025-11-25 09:16:01 -05:00
Leon Senft
4845a33018 refactor(forms): support custom control directives
Support binding `[field]` to directives that implement
`FormValueControl` or `FormCheckboxControl`.

The `[field]` binds to whichever directive (or component) matches first in the
event there are multiple implementations. We are considering whether to make
this an error state, which could be reported during type checking.

Closes #63910, Closes #64992

(cherry picked from commit f97a1d4856)
2025-11-24 13:48:20 -05:00
Miles Malerba
de5fca94c5 fix(forms): run reset as untracked
Run the signal forms `reset()` as untracked so it does not trigger
`effect` to rerun when the model changes

Fixes https://github.com/angular/angular/issues/65322

(cherry picked from commit 7ddf4a6b07)
2025-11-19 22:27:55 +00:00
Matthieu Riegler
91d8d55a80 fix(forms): Set error message of a schema error.
Use the error message of the issue as the error message of the error itself.

fixes #65247

(cherry picked from commit b41a94bc85)
2025-11-17 17:41:57 +00:00
Leon Senft
e682f00e29 refactor(forms): reduce boilerplate needed to define custom controls
An early piece of feedback received regarding custom controls hosted on
native inputs was that they required a lot of boilerplate to bind
`FieldState` properties. Each property required an input to accept the
property, and a host binding to forward it to the native control.

(cherry picked from commit c727df5d38)
2025-11-17 17:41:02 +00:00
Leon Senft
d89e522a1f fix(forms): debounce updates from interop controls
* Apply any debounce rules to updates from interop controls (if configured).
* Add tests to ensure debouncing works for all control types (native, custom,
  and interop).

(cherry picked from commit b1037ec2f0)
2025-11-17 16:37:29 +00:00
Miles Malerba
e1a7c35370 refactor(forms): improve typing on min & max (#65212)
If we're calling `min` on a path that's guaranteed to be `number` we
don't want to make the users validator function handle the `null` or
`string` cases.

This uncovered an issue in the `SchemaTreePath` type which needed to be
fixed by preventing the model type from being distributed over.

PR Close #65212
2025-11-14 21:56:58 +00:00
Miles Malerba
ff4633dab8 refactor(forms): allow passing number|string|null paths to min & max (#65212)
Relaxes the constraints on which paths can be used with the `min` &
`max` validation rules, since people may want to validate a
potentially-null number, or a numeric value represented as a string

PR Close #65212
2025-11-14 21:56:58 +00:00
Miles Malerba
966bfae337 build(forms): expose signal forms compat package
Hooks up @angular/forms/signals/compat to be released and have its docs
published

(cherry picked from commit fc1ef79ad4)
2025-11-14 17:23:39 +00:00
cexbrayat
1a8dbd79e6 docs: FormArrayDirective mentions
(cherry picked from commit b0dd813664)
2025-11-14 16:40:44 +00:00
Leon Senft
8e988124ab test(forms): [field] inputs on components should just pass through
Test that a component with a bound `[field]` input is not treated as a
control, and that `fieldBinding` does not include the corresponding
`Field` instance.

(cherry picked from commit acb78eeb7a)
2025-11-14 16:37:45 +00:00
Leon Senft
37463525be refactor(forms): use AbortSignal to cancel debounced updates
Add an `AbortSignal` parameter to `Debouncer`. Implementations may
choose to accept this parameter to be informed when a debounced
operation is aborted. This may be useful for canceling pending timers or
avoiding unnecessary work.

(cherry picked from commit 98ce9a7b17)
2025-11-13 17:59:22 +00:00
Leon Senft
a278ee358c feat(forms): add debounce() rule for signal forms
The `debounce()` rule allows developers to control when changes to a
form control are synchronized to the form model.

This feature necessitated some changes to `FieldState`:

  * `controlValue` is a new signal property that represents the current
    value of a form field as it appears in its corresponding control.

  * `value` conceptually remains unchanged; however, its value may lag
    behind that of `controlValue` if a `debounce()` rule is applied.

The `debounce()` rule essentially manages when changes to `controlValue` are
synchronized to `value`. The intent is that an expensive or slow
validation rule can react to the debounced `value`, rather than a more
frequently changing `controlValue`.

Directly updating `value` immediately updates `controlValue`, and cancels any
pending debounced updates.

When multiple `debounce()` rules are applied to the same field, the last
currently active rule is used to debounce an update. These rules are
applied to child fields as well, unless they override them with their
own rule.

(cherry picked from commit d337cfb68f)
2025-11-11 12:00:13 -08:00
Leon Senft
33ed7d116b refactor(forms): do not infer accumulated metadata type from initial value
This removes the need to specify type arguments for
`reducedMetadataKey()` when the value returned from the `getIntial`
callback is a subtype of the accumulated type.

(cherry picked from commit 8866934334)
2025-11-11 12:00:12 -08:00
cexbrayat
eea81c2733 docs: remove error mentions in signal forms docs
`error` no longer exists and is called `validate`

(cherry picked from commit 800b01f5a1)
2025-11-11 08:30:13 -08:00
arturovt
8ab084754b refactor(core): mark VERSION as @__PURE__ for better tree-shaking
Annotate the `new Version(...)` call with `/* @__PURE__ */` to signal to optimizers that the constructor is side-effect free.

Without this hint, bundlers such as Terser or ESBuild may conservatively retain the `VERSION` instantiation even when unused. With the annotation, the constant can be tree-shaken away in production builds if not referenced, reducing bundle size.

(cherry picked from commit d3f67f6ca8)
2025-11-10 12:04:09 -08:00
SkyZeroZx
511200265b docs: improve discoverability of forms
(cherry picked from commit e3fc57e8fc)
2025-11-10 07:57:48 -08:00
Leon Senft
781a3299f9 perf(forms): only update interop controls when bound field changes
https://github.com/angular/angular/pull/64590 implemented change
detection for field bindings, but only for those bound to native or
custom form controls. This change extends that optimization to apply to
field bindings on interoperable controls built using Reactive Forms as well.

(cherry picked from commit 850f0d6b3d)
2025-11-07 11:58:20 -08:00
Kristiyan Kostadinov
cc25f96f02 test(forms): fix type checking errors in signal form tests
Fixes some type checking errors in the signal forms tests that accumulated while there wasn't any type checking.

(cherry picked from commit 10915f2f5f)
2025-11-07 11:57:53 -08:00
Miles Malerba
a3cb1dfd1a refactor(forms): nicer type errors
By intersecting with `object` instead of `unknown` in the primitive and
`FormControl` cases, we get TypeScript to show nicer type errors that
mention `FieldTree<...>` insetad of `() => FieldState<...>`

(cherry picked from commit 0efb3f6d1f)
2025-11-07 15:45:08 +00:00
Miles Malerba
6d6fda8610 refactor(forms): allow FieldTree of recursive type
Adjusts our typings to work better with recursive types and avoid
infinite recursion in the type system.

(cherry picked from commit faccf03ee0)
2025-11-06 21:43:46 +00:00
Miles Malerba
9ea1c2216d refactor(forms): expose pathKeys as part of the API
Currently we maintain the pathKeys internally, but do not expose them
through the `FieldContext`, this PR updates the `FieldContext` to expose
this property.

(cherry picked from commit 2fd8dc9195)
2025-11-06 21:43:15 +00:00
Miles Malerba
8b4195fe7e refactor(forms): support dynamic object logic
extends `applyEach` to work on objects as well, conditionally applying
logic to each property of the object.

(cherry picked from commit c9058087ae)
2025-11-06 21:42:51 +00:00
arturovt
e90f9e418c refactor(forms): tree-shake ngControlStatusHost and ngGroupStatusHost
This commit removes `ngGroupStatusHost` variable because it's a side-effect, ending up preserving `ngControlStatusHost` and `ngGroupStatusHost`.

(cherry picked from commit 9f76fb61df)
2025-11-06 18:57:48 +00:00
kirjs
3bbcae6ce8 refactor(forms): add compatForm
This allows using reactive form controls in signal forms

(cherry picked from commit 60447945bc)
2025-11-06 18:51:31 +00:00
Alan Agius
3bed9f0f16
build: format md files
This commit configures prettier to format markdown files.
2025-11-06 10:07:13 -08:00
Miles Malerba
60ce31fb62 test(forms): re-enable some disabled tests
Adjusts test behavior to reflect current implementation and enables them

(cherry picked from commit fa02d96508)
2025-11-06 17:41:31 +00:00
Jessica Janiuk
a3c2fe819a Revert "refactor(common): Removes unused imports to clean up dependencies"
This reverts commit 6d3e0f1a51.
2025-11-06 09:05:05 -08:00
SkyZeroZx
6d3e0f1a51 refactor(common): Removes unused imports to clean up dependencies
Eliminates unnecessary imports to reduce clutter and improve maintainability

(cherry picked from commit ca3ef38143)
2025-11-06 16:35:32 +00:00
Miles Malerba
f3e2252f4b refactor(forms): add better support for Object.keys and Symbol.iterator
Improves support for using `Object.keys` (and related methods) and
`Symbol.iterator` to work with a `FieldTree` of unknown shape.

(cherry picked from commit e87f423d90)
2025-11-06 15:51:31 +00:00
Miles Malerba
d1f4c8c18c refactor(forms): improved select support
When we set the `value` on a `<select>` element we're really just
setting the selected `<option>`. It treats the selected option as the
source of truth, rather than the actual value. This means that if
options are added or removed or their value changes, the `<select>` may
wind up with a different `value` than what's in our model.

This PR resolves this issue by adding a `MutationObserver` to the select
that is used to resync its value to the model whenever the options may
have changed.

(cherry picked from commit 194b41199b)
2025-11-06 15:49:09 +00:00
Miles Malerba
c3b4bea951 refactor(forms): improve discoverability of ValidationError flavors (#64747)
Improves discoverability by putting the WithField, WithoutField, etc as
subtypes of the main ValidationError type

PR Close #64747
2025-11-05 22:42:59 +00:00
Leon Senft
507b3466ee perf(forms): implement change detection for field control bindings
For each field state property, check if it has changed since the last
time it was checked before writing it the corresponding form control
property.

The `pattern` and `required` properties of the field state now return a
default value rather than `undefined` if not defined by metadata.

(cherry picked from commit 41be02da2f)
2025-11-04 16:11:33 -08:00
SkyZeroZx
d3ed745653 refactor(forms): remove redundant providedIn: 'root' from injection tokens
Removes unnecessary `providedIn: 'root'` declarations from injection tokens

(cherry picked from commit 78e6716f40)
2025-11-03 16:31:58 -08:00
Kristiyan Kostadinov
a4fe078969 refactor(common): remove unused import (#64699)
Cleans up unused code to improve readability and maintainability.

PR Close #64699
2025-10-30 19:27:34 +00:00
Miles Malerba
981fabc9b6 feat(docs-infra): add support for namespaces
Adds support for generating docs for namespaces (and merged declarations
of namespace + type)

(cherry picked from commit 662f0e5c00)
2025-10-29 13:22:26 -07:00
Miles Malerba
9cecae81de docs(forms): make FIELD experimental
Adds `@experimental` tag to `FIELD`

(cherry picked from commit a7180b65db)
2025-10-29 10:40:53 +01:00
Miles Malerba
e0a9cfc973 refactor(forms): rename the logic property on FieldPathNode
`logic` was a confusing name, as it is a logic builder instance, not a
logic instance.

(cherry picked from commit 3d70d68676)
2025-10-29 10:40:10 +01:00
Miles Malerba
60c8e2779c refactor(forms): preserve logic order when apply is used on root path
In some cases the logic order was not preserved properly when using `apply`. In particular this occurs when some logic is registered on a child of the root, followed by an apply to the root, followed by further logic registered on a child. In this case the final registered logic wound up running before the applied logic.

This happened because `FieldPathNode` for a child path was caching its `LogicNodeBuilder` at creation time. This meant that if the parent's `LogicNodeBuilder` changed (e.g., due to an `apply` call), the child would still be using the old one.

This commit fixes the issue by dynamically resolving the `LogicNodeBuilder` for a child path whenever it is accessed.

(cherry picked from commit fa13a167f1)
2025-10-29 10:40:09 +01:00
Miles Malerba
07e56596a5 refactor(forms): track arrays in a parent array by index
This commit changes arrays in a parent array to be tracked the same way
as primitive values like strings and numbers. This is necessary because
the tracking key symbol used to maintain identity for objects in an
array does not survive the array spread operation:

```
return {...oldValue} // tracking symbol preserved 
return [...oldValue] // tracking symbol lost 
```

(cherry picked from commit f80b51a738)
2025-10-28 20:52:54 +01:00
kirjs
d74fb923cc refactor(forms): Allow returning plain values from validators
This makes the API nicer to use

(cherry picked from commit c57bbaa87d)
2025-10-28 20:50:48 +01:00
kirjs
07b9be1d2c refactor(forms): Allow returning plain values from validators
This makes the API nicer to use

(cherry picked from commit 8562e29be4)
2025-10-28 20:50:48 +01:00
kirjs
71a6266dd3 refactor(forms): Allow returning plain values from validators
This makes the API nicer to use

(cherry picked from commit 28847c9a19)
2025-10-28 20:50:48 +01:00
kirjs
2c2ea2c0da refactor(forms): Allow returning plain values from validators
This makes the API nicer to use

(cherry picked from commit b5c29d0d0a)
2025-10-28 20:50:48 +01:00
csorrentino
bd780366e8 refactor(forms): add onError callback to validateHttp for HTTP errors
Adds onError callback inside validateHttp validator in signal forms

PR-Close: #63949
(cherry picked from commit a5678f6f2b)
2025-10-27 17:12:28 +01:00
Miles Malerba
22c012c985 refactor(forms): fix some typos
Fix some typos in signal forms code

(cherry picked from commit 01e1ad2b37)
2025-10-27 09:27:05 +01:00
Matthieu Riegler
a1ac9a6415 fix(forms): interop supports CVAs with signals (#64618)
The directive implemnetation might set CVA values during the template evaluation. Since the template is a reactive context we need to untrack when setting the CVA values to prevent writing to signals in a reactive context.

fixes #64614

PR Close #64618
2025-10-24 09:31:39 +02:00
Miles Malerba
0af83e6855 test(forms): test reactive iteration over array field (#64113)
Adds a test that verifies @for iteration over an array field is actually
reactive to new items being added to the array.

PR Close #64113
2025-10-24 09:29:29 +02:00