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
##### Approach #2: Use `resolve`to access other fields' values
267
+
##### Approach #2: Use helper functions to access other fields' state or values
268
268
269
-
If you need to access other fields' values but don't want to move the logic to a common parent node, you can define the logic on the desired field and then use the `resolve` function provided in the `FieldContext`to access the state of _other_ fields.
269
+
If you need to access other fields' values but don't want to move the logic to a common parent node, you can define the logic on the desired field. The `FieldContext` provides helper functions to access the state or value of _other_ fields:
270
270
271
-
The `resolve()` function takes another `FieldPath` from anywhere in your field structure and returns the corresponding `Field` instance for that path. You can then access its `$state` to get its value, state, etc.
271
+
-**`valueOf(otherPath: FieldPath<U>): U`**: Directly retrieves the current value of the field at `otherPath`.
272
+
-**`stateOf(otherPath: FieldPath<U>): FieldState<U>`**: Retrieves the `FieldState` instance for the field at `otherPath` (e.g., `stateOf(otherPath).disabled()`).
273
+
--**`fieldOf(otherPath: FieldPath<U>): Field<U>`**: Retrieves the `Field` instance for the field at `otherPath`. This is useful when you want to access its decendants or specific array items.
272
274
273
-
Here's the same password matching validation, but associating the error with the `confirm` field instead:
275
+
Here's the same password matching validation, but associating the error with the `confirm` field instead, using `valueOf`:
passwordForm.confirm.$state.errors(); // [{kind: 'non-matching', message: 'Password and confirm must match'}]
296
298
```
297
299
298
-
Note that because `value()`in `resolve(path.password).$state.value()`is a signal read, this establishes a reactive dependency on the value of `password` as well as the value of `confirm`, ensuring that the validation is recomputed if either one changes.
300
+
Note that because `valueOf(path.password)`reads a signal internally (as does `value()`for the current field), this establishes a reactive dependency on the value of `password` as well as the value of `confirm`, ensuring that the validation is recomputed if either one changes.
@@ -505,8 +507,8 @@ Angular Signal Forms provides a `submit()` helper function to manage this workfl
505
507
506
508
1.**`field`**: The `Field` instance to submit. This can be the root field or any sub-field node.
507
509
2.**`action`**: An asynchronous function that performs the submission action. It receives the `field` being submitted as an argument and returns a `Promise`.
508
-
- The returned `Promise` resolves with `void` (or `undefined`, or `[]`) if the action completes successfully without server-side validation errors.
509
-
- It resolves with an array of `ServerError` if the submission fails due to server-side validation or other issues that need to be reported back onto the form fields. The `ServerError` structure is detailed in the next section.
510
+
- The returned `Promise` resolves with `void` (or `undefined`, or `[]`) if the action completes successfully without server-side validation errors.
511
+
- It resolves with an array of `ServerError` if the submission fails due to server-side validation or other issues that need to be reported back onto the form fields. The `ServerError` structure is detailed in the next section.
510
512
511
513
All `FieldState` objects have a `submittedStatus` signal that indicates their current submit state. The status can be `'unsubmitted'`, `'submitting'`, or `'submitted'`. There is no status to indicate that the submit errored because errors are reported through the `errors()` state the same way as client validation errors. (This is discussed more in the next section). `FieldState` objects also have a `resetSubmittedStatus()` method which sets the `submittedStatus` back to `'unsubmitted'`.
512
514
@@ -627,4 +629,4 @@ The `[field]` directive works out-of-the-box with standard HTML form elements li
627
629
628
630
It can also integrate with custom form components (including those from libraries like Angular Material - e.g., `<mat-select>`, `<mat-radio>`) provided they correctly implement Angular's `ControlValueAccessor` interface. This is the standard mechanism in Angular for components to participate in forms.
629
631
630
-
<!-- TODO: add a more indepth section on how to integrate your own custom UI controls -->
632
+
<!-- TODO: add a more in-depth section on how to integrate your own custom UI controls -->
@@ -345,9 +347,9 @@ export class FeedbackComponent {
345
347
346
348
Now let's write a custom validator to ensure that the password and confirmation password match.
347
349
348
-
To do this, we will use the special `resolve` function provided to the validator. `resolve` takes a path segment and returns the corresponding form field instance.
350
+
To do this, we will use the special `valueOf` function provided to the validator. `valueOf` takes a path segment and returns the corresponding form field's value.
349
351
350
-
> `resolve` can be used for cross-field validation.
352
+
> Besides `valueOf`, you can also use `stateOf` and `fieldOf` can be used for cross-field validation.
351
353
352
354
```typescript
353
355
// feedback.ts
@@ -359,8 +361,8 @@ export class FeedbackComponent {
0 commit comments