You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit a2337f7
Browse filesBrowse the repository at this point in the historyBrowse files
docs: add global form state signals to signal forms guide
Add a new section for 'Form State Signals' in the signal forms essential guide. This documents global signals such as dirty(), valid(), invalid(), pending(), and touched() at the form level, as requested in issue angular#69544.
Fixesangular#69544
### 2. Pass the form model to `form()` to create a `FieldTree`
25
+
### 2. Create a `FieldTree` with `form()`
26
26
27
-
Then, you pass your form model into the `form()` function to create a **field tree** - an object structure that mirrors your model's shape, allowing you to access fields with dot notation:
27
+
Then, you pass your form model into the `form()` function to create a **field tree** - an object structure that mirrors your model's shape, allowing you to access fields with dot notation.
28
+
29
+
Both the root form object and its nested properties are `FieldTree` nodes:
28
30
29
31
```ts
30
32
const loginForm =form(loginModel);
31
33
32
-
// Access fields directly by property name
33
-
loginForm.email;
34
-
loginForm.password;
34
+
// loginForm is a FieldTree
35
+
// loginForm.email is also a FieldTree
35
36
```
36
37
37
38
### 3. Bind HTML inputs with `[formField]` directive
@@ -47,18 +48,20 @@ As a result, user changes (such as typing in the field) automatically updates th
47
48
48
49
NOTE: The `[formField]` directive also syncs field state for attributes like `required`, `disabled`, and `readonly` when appropriate.
49
50
50
-
### 4. Read field values with `value()`
51
+
### 4. Read state with `FieldTree` signals
51
52
52
-
You can access field state by calling the field as a function. This returns a `FieldState` object containing reactive signals for the field's value, validation status, and interaction state:
53
+
You can access state for any part of the tree by calling the `FieldTree` node as a function. This returns a state object containing reactive signals for the value, validation status, and interaction state:
53
54
54
55
```ts
55
-
loginForm.email(); // Returns FieldState with value(), valid(), touched(), etc.
56
+
loginForm(); // Returns state for the whole form
57
+
loginForm.email(); // Returns state for the email field
56
58
```
57
59
58
-
To read the field's current value, access the `value()` signal:
60
+
To read the current value, access the `value()` signal:
59
61
60
62
```html
61
-
<!-- Render form value that updates automatically as user types -->
63
+
<!-- Render values that update automatically as user types -->
@@ -67,9 +70,9 @@ To read the field's current value, access the `value()` signal:
67
70
const currentEmail =loginForm.email().value();
68
71
```
69
72
70
-
### 5. Update field values with `set()`
73
+
### 5. Update values with `set()`
71
74
72
-
You can programmatically update a field's value using the `value.set()` method. This updates both the field and the underlying model signal:
75
+
You can programmatically update values using the `value.set()` method on any node. This updates both the `FieldTree` and the underlying model signal:
73
76
74
77
```ts
75
78
// Update the value programmatically
@@ -235,7 +238,22 @@ required(schemaPath.email, {message: 'Email is required'});
235
238
email(schemaPath.email, {message: 'Please enter a valid email address'});
236
239
```
237
240
238
-
Each form field exposes its validation state through signals. For example, you can check `field().valid()` to see if validation passes, `field().touched()` to see if the user has interacted with it, and `field().errors()` to get the list of validation errors.
241
+
Each node in the `FieldTree` exposes its validation and interaction state through reactive signals.
242
+
243
+
### FieldTree State Signals
244
+
245
+
Every node in the tree, including the root form object, provides the same signals to track its state. Since every node is a `FieldTree`, the API for monitoring validity and interaction is identical at every level.
0 commit comments