Skip to content

Commit 7d985ce

Browse files
kirjsthePunderWoman
authored andcommitted
docs(forms): Clarify returning errors from submit functions
Update outdate comment, and add a section to the docs
1 parent a7e470a commit 7d985ce

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

adev/src/content/guide/forms/signals/field-state-management.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ When checking validity in code, use `invalid()` instead of `!valid()` if you wan
109109

110110
Access the array of validation errors with `errors()`. Each error object contains:
111111

112-
| Property | Description |
113-
| --------- | --------------------------------------------------------------- |
114-
| `kind` | The validation rule that failed (such as "required" or "email") |
115-
| `message` | Optional human-readable error message |
116-
| `field` | Reference to the `FieldTree` where the error occurred |
112+
| Property | Description |
113+
| ----------- | --------------------------------------------------------------- |
114+
| `kind` | The validation rule that failed (such as "required" or "email") |
115+
| `message` | Optional human-readable error message |
116+
| `fieldTree` | Reference to the `FieldTree` where the error occurred |
117117

118118
NOTE: The `message` property is optional. Validators can provide custom error messages, but if not specified, you may need to map error `kind` values to your own messages.
119119

adev/src/content/guide/forms/signals/validation.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,19 @@ export class UrlFormComponent {
461461
}
462462
```
463463

464+
For submission errors that target specific fields, use the `fieldTree` property:
465+
466+
```ts
467+
// In a submit function
468+
return [
469+
{
470+
fieldTree: registrationForm.username, // Target specific field
471+
kind: 'server',
472+
message: 'Username already taken',
473+
},
474+
];
475+
```
476+
464477
The validator function receives a `FieldContext` object with:
465478

466479
| Property | Type | Description |

packages/forms/signals/src/api/structure.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export function applyWhenValue(
332332
/**
333333
* Submits a given `FieldTree` using the given action function and applies any submission errors
334334
* resulting from the action to the field. Submission errors returned by the `action` will be integrated
335-
* into the field as a `ValidationError` on the sub-field indicated by the `field` property of the
335+
* into the field as a `ValidationError` on the sub-field indicated by the `fieldTree` property of the
336336
* submission error.
337337
*
338338
* @example
@@ -341,8 +341,9 @@ export function applyWhenValue(
341341
* const result = await myClient.registerNewUser(registrationForm().value());
342342
* if (result.errorCode === myClient.ErrorCode.USERNAME_TAKEN) {
343343
* return [{
344-
* field: registrationForm.username,
345-
* error: {kind: 'server', message: 'Username already taken'}
344+
* fieldTree: registrationForm.username,
345+
* kind: 'server',
346+
* message: 'Username already taken'
346347
* }];
347348
* }
348349
* return undefined;

0 commit comments

Comments
 (0)