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
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -451,6 +451,43 @@ Alternatively, you could use `select` and `select multiple`:
451
451
452
452
> [!NOTE] As with unchecked `checkbox` inputs, if no selections are made then the data will be `undefined`. For this reason, the `languages` field uses `v.optional(v.array(...), [])` rather than just `v.array(...)`.
453
453
454
+
### Programmatic validation
455
+
456
+
In addition to declarative schema validation, you can programmatically mark fields as invalid inside the form handler using the `invalid` function. This is useful for cases where you can't know if something is valid until you try to perform some action:
457
+
458
+
```js
459
+
/// file: src/routes/shop/data.remote.js
460
+
import*asvfrom'valibot';
461
+
import { form } from'$app/server';
462
+
import*asdbfrom'$lib/server/database';
463
+
464
+
exportconstbuyHotcakes=form(
465
+
v.object({
466
+
qty:v.pipe(
467
+
v.number(),
468
+
v.minValue(1, 'you must buy at least one hotcake')
469
+
)
470
+
}),
471
+
async (data, invalid) => {
472
+
try {
473
+
awaitdb.buy(data.qty);
474
+
} catch (e) {
475
+
if (e.code==='OUT_OF_STOCK') {
476
+
invalid(
477
+
invalid.qty(`we don't have enough hotcakes`)
478
+
);
479
+
}
480
+
}
481
+
}
482
+
);
483
+
```
484
+
485
+
The `invalid` function works as both a function and a proxy:
486
+
487
+
- Call `invalid(issue1, issue2, ...issueN)` to throw a validation error
488
+
- If an issue is a `string`, it applies to the form as a whole (and will show up in `fields.allIssues()`)
489
+
- Use `invalid.fieldName(message)` to create an issue for a specific field. Like `fields` this is type-safe and you can use regular property access syntax to create issues for deeply nested objects (e.g. `invalid.profile.email('Email already exists')` or `invalid.items[0].qty('Insufficient stock')`)
490
+
454
491
### Validation
455
492
456
493
If the submitted data doesn't pass the schema, the callback will not run. Instead, each invalid field's `issues()` method will return an array of `{ message: string }` objects, and the `aria-invalid` attribute (returned from `as(...)`) will be set to `true`:
@@ -756,6 +793,27 @@ await submit().updates(
756
793
757
794
The override will be applied immediately, and released when the submission completes (or fails).
758
795
796
+
### Multiple instances of a form
797
+
798
+
Some forms may be repeated as part of a list. In this case you can create separate instances of a form function via `for(id)` to achieve isolation.
799
+
800
+
```svelte
801
+
<!--- file: src/routes/todos/+page.svelte --->
802
+
<script>
803
+
import { getTodos, modifyTodo } from '../data.remote';
By default, submitting a form will send a request to the URL indicated by the `<form>` element's [`action`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/form#attributes_for_form_submission) attribute, which in the case of a remote function is a property on the form object generated by SvelteKit.
0 commit comments