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
The `form` function makes it easy to write data to the server. It takes a callback that receives the current [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)...
231
+
The `form` function makes it easy to write data to the server. It takes a callback that receives `data` constructed from the submitted [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)...
The form object contains `method` and `action` properties that allow it to work without JavaScript (i.e. it submits data and reloads the page). It also has an `onsubmit` handler that progressively enhances the form when JavaScript is available, submitting data *without* reloading the entire page.
312
+
As with `query`, if the callback uses the submitted `data`, it should be [validated](#query-Query-arguments) by passing a [Standard Schema](https://standardschema.dev) as the first argument to `form`. The one difference is to `query` is that the schema inputs must all be of type `string` or `File`, since that's all the original `FormData` provides. You can however coerce the value into a different type — how to do that depends on the validation library you use.
The `name` attributes on the form controls must correspond to the properties of the schema — `title` and `content` in this case. If you schema contains objects, use object notation:
If you'd like type safety and autocomplete when setting `name` attributes, use the form object's `field` method:
360
+
361
+
```svelte
362
+
<label>
363
+
<h2>Title</h2>
364
+
<input name={+++createPost.field('title')+++} />
365
+
</label>
366
+
```
367
+
368
+
This will error during typechecking if `title` does not exist on your schema.
369
+
370
+
The form object contains `method` and `action` properties that allow it to work without JavaScript (i.e. it submits data and reloads the page). It also has an [attachment](/docs/svelte/@attach) that progressively enhances the form when JavaScript is available, submitting data *without* reloading the entire page.
371
+
372
+
### Validation
373
+
374
+
If the submitted data doesn't pass the schema, the callback will not run. Instead, the form object's `issues` object will be populated:
375
+
376
+
```svelte
377
+
<form {...createPost}>
378
+
<label>
379
+
<h2>Title</h2>
380
+
381
+
+++ {#ifcreatePost.issues.title}
382
+
{#each createPost.issues.title as issue}
383
+
<p class="issue">{issue.message}</p>
384
+
{/each}
385
+
{/if}+++
386
+
387
+
<input
388
+
name="title"
389
+
+++aria-invalid={!!createPost.issues.title}+++
390
+
/>
391
+
</label>
392
+
393
+
<label>
394
+
<h2>Write your post</h2>
395
+
396
+
+++ {#ifcreatePost.issues.content}
397
+
{#each createPost.issues.content as issue}
398
+
<p class="issue">{issue.message}</p>
399
+
{/each}
400
+
{/if}+++
401
+
402
+
<textarea
403
+
name="content"
404
+
+++aria-invalid={!!createPost.issues.content}+++
405
+
></textarea>
406
+
</label>
407
+
408
+
<button>Publish!</button>
409
+
</form>
410
+
```
411
+
412
+
You don't need to wait until the form is submitted to validate the data — you can call `validate()` programmatically, for example in an `oninput` callback (which will validate the data on every keystroke) or an `onchange` callback:
By default, issues will be ignored if they belong to form controls that haven't yet been interacted with. To validate _all_ inputs, call `validate({ includeUntouched:true })`.
421
+
422
+
For client-side validation, you can specify a _preflight_ schema which will populate `issues` and prevent data being sent to the server if the data doesn't validate:
423
+
424
+
```svelte
425
+
<script>
426
+
import*asvfrom'valibot';
427
+
import { createPost } from'../data.remote';
428
+
429
+
constschema=v.object({
430
+
title:v.pipe(v.string(), v.nonEmpty()),
431
+
content:v.pipe(v.string(), v.nonEmpty())
432
+
});
433
+
</script>
434
+
435
+
<h1>Create a newpost</h1>
436
+
437
+
<form {...+++createPost.preflight(schema)+++}>
438
+
<!---->
439
+
</form>
440
+
```
441
+
442
+
> [!NOTE] The preflight schema can be the same object as your server-side schema, if appropriate, though it won't be able to do server-side checks like 'this value already exists in the database'. Note that you cannot export a schema from a `.remote.ts` or `.remote.js` file, so the schema must either be exported from a shared module, or from a `<script module>` block in the component containing the `<form>`.
443
+
444
+
### Live inputs
445
+
446
+
The form object contains a `input` property which reflects its current value. As the user interacts with the form, `input` is automatically updated:
In the case of a non-progressively-enhanced form submission (i.e. where JavaScript is unavailable, for whatever reason) `input` is also populated if the submitted data is invalid, so that the user does not need to fill the entire form out from scratch.
462
+
463
+
You can prevent sensitive data (such as passwords and credit card numbers) from being sent back to the user by using a name with a leading underscore:
464
+
465
+
```svelte
466
+
<form {...register}>
467
+
<label>
468
+
Username
469
+
<input
470
+
name="username"
471
+
value={register.input.username}
472
+
aria-invalid={!!register.issues.username}
473
+
/>
474
+
</label>
475
+
476
+
<label>
477
+
Password
478
+
<input
479
+
type="password"
480
+
+++name="_password"+++
481
+
+++aria-invalid={!!register.issues._password}+++
482
+
/>
483
+
</label>
484
+
485
+
<button>Sign up!</button>
486
+
</form>
487
+
```
488
+
489
+
In this example, if the data does not validate, only the first `<input>` will be populated when the page reloads.
@@ -439,9 +625,7 @@ We can customize what happens when the form is submitted with the `enhance` meth
439
625
showToast('Oh no! Something went wrong');
440
626
}
441
627
})}>
442
-
<input name="title"/>
443
-
<textarea name="content"></textarea>
444
-
<button>publish</button>
628
+
<!---->
445
629
</form>
446
630
```
447
631
@@ -518,7 +702,7 @@ The `command` function, like `form`, allows you to write data to the server. Unl
518
702
519
703
> [!NOTE] Prefer `form` where possible, since it gracefully degrades if JavaScript is disabled or fails to load.
520
704
521
-
As with `query`, if the function accepts an argument, it should be [validated](#query-Query-arguments) by passing a [Standard Schema](https://standardschema.dev) as the first argument to `command`.
705
+
As with `query` and `form`, if the function accepts an argument, it should be [validated](#query-Query-arguments) by passing a [Standard Schema](https://standardschema.dev) as the first argument to `command`.
0 commit comments