Form
A validated form.
Anatomy
Pair Form with Field:
<script>
import { Form } from '@shardsui/svelte/form'
import { Field } from '@shardsui/svelte/field'
</script>
<Form>
<Field.Root>
<Field.Label />
<Field.Control />
<Field.Error />
</Field.Root>
</Form>Examples
Validating from code
The component instance, obtained with bind:this, exposes validate(). It runs every field; pass
a field name to run just that one.
<script>
let form = $state()
</script>
<Form bind:this={form}>
<Field.Root name="email">
<Field.Control type="email" required />
<Field.Error />
</Field.Root>
<button type="button" onclick={() => form.validate('email')}>Check email</button>
</Form>The name matched is the one on Field.Root, falling back to the name on the control.
API reference
Renders a <form> element. Submitting validates every field first; if one fails, the native submit
is cancelled and focus moves to the first invalid control, selecting its text when it is an
<input>.
The component instance, obtained with bind:this, exposes:
<Form> is generic over FormValues extends Record<string, unknown>, which defaults to Record<string, unknown>. Set it to type the object onFormSubmit receives.
Other standard <form> attributes (method, action, target, enctype) pass through.