Form
SvForm: a schema-driven signup form wiring the whole kit (text, email, password, select, date, switch, rating) with required + cross-field custom validation in a two-column grid.
A live, editable Svelte 5 component example from the SvGrid gallery (Layout). Read the SvGrid UI component docs for the full API.
About this example
SvForm in Svelte 5 as a schema-driven sign-up form: text, email, password, select, date, switch and rating fields declared in one fields array, required and cross-field custom validation, initial values and a responsive two-column layout, with the submitted values shown as JSON. It is the fastest way to a validated form from the SvGrid UI kit.
SvForm - a schema-driven signup form that wires the whole kit (text, email, password, select, date, switch, rating) with required + cross-field custom validation, in a responsive two-column grid. Copy-paste ready.
Imports, features and API used
Imports: @svgrid/grid
Frequently asked questions
How do I add validation?
Set required on a field or give it a validate function that receives the value and all values and returns a message; cross-field rules such as confirm password read the other value there.
What does onSubmit receive?
An object of field name to value after every rule passes; the demo prints it as JSON. Nothing is called while errors remain.
How do I pre-fill the form?
Pass initial with the values to start from; fields not listed start empty or at the type's default.
Related documentation
Related articles
- Avoiding Layout Thrash in Custom Grid Cells - Layout thrash from interleaved DOM reads and writes is the most common cause of scroll jank in grids with custom cells - here is how to find it and design your way out of it.
- Throttling Live Grid Updates to Animation Frames - High-frequency WebSocket feeds will wreck grid performance if you apply every message immediately. One rAF buffer and a Map coalescer cuts thousands of renders down to 60 per second.
- Stable Row Identity in SvGrid (and why it matters) - How SvGrid tracks rows across mutations, why object reference stability is the key to correct selection and editing, and the exact patterns to use with live data feeds.
Source code (322-form.svelte)
<script lang="ts">
/**
* SvForm - a schema-driven signup form that wires the whole kit (text, email,
* password, select, date, switch, rating) with required + cross-field custom
* validation, in a responsive two-column grid. Copy-paste ready.
*/
import { SvForm, rules } from '@svgrid/grid'
import type { FormField } from '@svgrid/grid'
// Declarative rules from the shared library - email / minLength / cross-field
// compare - instead of hand-written closures.
const fields: FormField[] = [
{ name: 'firstName', label: 'First name', required: true },
{ name: 'lastName', label: 'Last name', required: true },
{ name: 'email', label: 'Work email', type: 'email', required: true, full: true,
rules: [rules.email()] },
{ name: 'password', label: 'Password', type: 'password', required: true,
rules: [rules.minLength(8), rules.pattern(/\d/, { message: 'Include a number' })] },
{ name: 'confirm', label: 'Confirm password', type: 'password', required: true,
rules: [rules.compare('password', '===', { message: 'Passwords do not match' })] },
{ name: 'role', label: 'Role', type: 'select', required: true, options: [
{ value: 'dev', label: 'Developer' }, { value: 'design', label: 'Designer' },
{ value: 'pm', label: 'Product manager' }, { value: 'other', label: 'Other' },
] },
{ name: 'startDate', label: 'Start date', type: 'date' },
{ name: 'newsletter', label: 'Send me product updates', type: 'switch' },
{ name: 'experience', label: 'Rate your Svelte experience', type: 'rating', full: true },
]
let result = $state<string>('')
</script>
<div class="wrap">
<header>
<h2>Form</h2>
<p>A schema-driven form: declare fields, get labels, the right control per type, required + custom (cross-field) validation and a gated submit.</p>
</header>
<div class="card">
<SvForm {fields} columns={2} submitLabel="Create account"
initial={{ role: 'dev', experience: 3 }}
onSubmit={(v) => { result = JSON.stringify(v, null, 2) }} />
</div>
{#if result}
<pre class="result">{result}</pre>
{/if}
</div>
<style>
.wrap { padding: 20px; max-width: 620px; display: flex; flex-direction: column; gap: 16px; }
header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
.card { padding: 20px; border: 1px solid var(--sg-border, #e2e8f0); border-radius: 12px; }
.result { margin: 0; padding: 14px; background: var(--sg-header-bg, #f8fafc); border: 1px solid var(--sg-border, #e2e8f0); border-radius: 10px; font-size: 12px; overflow: auto; }
</style>More Layout examples
- Account & security settings console - A real SaaS settings surface composed from the UI kit: SvMenubar app bar, promise + Undo-action toasts on save, SvPopconfirm on destructive rows, SvHoverCard teammate previews, and frame input adornments (leading icons, prefix affixes, masked API key). Profile / Team / Security tabs over SvCard + SvStat.
- Invoice builder - An adornment-heavy money form: currency prefixes, % suffixes, masked tax IDs (frame adornments), line items with SvPopconfirm delete + Undo action toasts, live SvStat totals, an SvHoverCard tax hint, and a promise toast on Send. Pure UI-kit composition.
- Operations KPI dashboard - A KPI console: SvStat + SvSparkline tiles, an SvGauge SLA dial, SvHoverCard drill-down previews per service, an SvMenubar toolbar (View / Range / Actions) and a promise toast on Refresh. Pure UI-kit composition, no grid dependency.
- Form: rich field types - SvForm reaching the whole input suite from one schema - phone, country, mask, combobox, radio, slider, tags, datetime and file - plus per-field help text, a readonly field, column span, and a promise toast on submit.
- Form: cascading fields - SvForm dependent selects - a child list derives from the parent value via a function `options`, and `dependsOn` clears the child when the parent changes so a stale selection never lingers. Country -> State -> City.