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
### 2. Pass the form model to `form()` to create a `FieldTree`
28
28
29
29
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:
30
30
31
31
```ts
32
-
form(loginModel);
32
+
const loginForm =form(loginModel);
33
33
34
34
// Access fields directly by property name
35
35
loginForm.email
36
36
loginForm.password
37
37
```
38
38
39
-
### 3. Bind inputs with `[field]` directive
39
+
### 3. Bind HTML inputs with `[field]` directive
40
40
41
41
Next, you bind your HTML inputs to the form using the `[field]` directive, which creates two-way binding between them:
42
42
@@ -45,19 +45,11 @@ Next, you bind your HTML inputs to the form using the `[field]` directive, which
As a result, user changes (such as typing in the field) automatically updates the form, and any programmatic changes update the displayed value as well:
As a result, user changes (such as typing in the field) automatically updates the form.
57
49
58
50
NOTE: The `[field]` directive also syncs field state for attributes like `required`, `disabled`, and `readonly` when appropriate.
59
51
60
-
### 4. Read form field values with `value()`
52
+
### 4. Read field values with `value()`
61
53
62
54
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:
63
55
@@ -77,6 +69,22 @@ To read the field's current value, access the `value()` signal:
77
69
const currentEmail =loginForm.email().value();
78
70
```
79
71
72
+
### 5. Update field values with `set()`
73
+
74
+
You can programmatically update a field's value using the `value.set()` method. This updates both the field and the underlying model signal:
@@ -202,16 +210,17 @@ NOTE: Multiple select (`<select multiple>`) is not supported by the `[field]` di
202
210
203
211
## Validation and state
204
212
205
-
Signal Forms provides built-in validators that you can apply to your form fields. To add validation, pass a schema function as the second argument to `form()`. This function receives a **FieldPath** parameter that allows you to reference the fields in your form model:
213
+
Signal Forms provides built-in validators that you can apply to your form fields. To add validation, pass a schema function as the second argument to `form()`. This function receives a **schema path** parameter that allows you to reference the fields in your form model:
NOTE: FieldPath only mirrors the shape of your data and does not allow you to access value or any other state.
223
+
NOTE: The schema path parameter provides paths to your fields for applying validation rules. To access field values and state, use the field tree (such as `loginForm.email()`).
215
224
216
225
Common validators include:
217
226
@@ -224,8 +233,8 @@ Common validators include:
224
233
You can also customize error messages by passing an options object as the second argument to the validator:
225
234
226
235
```ts
227
-
required(p.email, { message: 'Email is required' });
228
-
email(p.email, { message: 'Please enter a valid email address' });
236
+
required(schemaPath.email, { message: 'Email is required' });
237
+
email(schemaPath.email, { message: 'Please enter a valid email address' });
229
238
```
230
239
231
240
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.
@@ -251,4 +260,4 @@ Every `field()` provides these state signals:
251
260
|`pending()`| Returns `true` if async validation is in progress |
252
261
|`errors()`| Returns an array of validation errors with `kind` and `message` properties |
253
262
254
-
TIP: Show errors only after `field().touched()` is true to avoid displaying validation messages before the user has interacted with a field.
263
+
TIP: Use the `debounce()` validation rule to delay error display until the user stops typing or leaves the field. This prevents showing errors while the user is still entering their input.
0 commit comments