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
@@ -418,11 +418,36 @@ When validation fails, `CommandForm`:
418
418
2. Converts issues into `errors` (per field) via `transformIssues`.
419
419
3. Stores the raw issue array in `issues` for programmatic access.
420
420
421
-
If the command throws an `HttpError` from SvelteKit, the helper looks for `err.body.issues` and merges them into the same structures. Any other error is forwarded to `onError` after clearing submission state. You can handle validation errors to populate this in your `hooks.server.ts`
421
+
If you need to manually set an error follow these steps.
422
+
423
+
1. Setup your hooks.server.ts file and add an error handler.
// Note: If you don't want bad actors seeing your validation issues, you can do an auth check here before returning
428
+
if (errorinstanceofSchemaValidationError) returnerrorasSchemaValidationError;
429
+
};
430
+
```
431
+
432
+
2. Throw a new `SchemaValidationError` inside of the command.
433
+
434
+
```typescript
435
+
exportconst test =command(schema, async (data) => {
436
+
const user =awaitdb.user.findFirst({ where: { email: data.email } });
437
+
if (!user)
438
+
thrownewSchemaValidationError([
439
+
{ path: ['email'], message: 'Name is invalid server error!!' }
440
+
]);
441
+
});
442
+
```
443
+
444
+
In the above example this will populate the `form.errors.email.message` field so you can display the error to the user on the client.
445
+
446
+
> If you do not add the custom error handler in step 1, you will not get any issues back. This is a SvelteKit design principle when dealing with Remote Functions!
422
447
423
448
## Manual Errors
424
449
425
-
You can add errors manually by using the `addErrors` method on the client or by throwing a `new SchemaValidationError` inside of the remote function.
450
+
You can add errors manually by using the `addErrors` method (client only) or by throwing a `new SchemaValidationError`.
> addError() does NOT throw an error, you will have to do that once you call it. If you want to throw an error, throw a new `SchemaValidationError`
476
+
450
477
## Contributing
451
478
452
479
Feel free to contribute by opening a PR with a detailed description of why you are wanting to change what you are changing. If it can be tested with Vitest, that is preferred.
0 commit comments