# 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.

Uses `@svgrid/grid` (MIT). Category: Layout. Live demo: https://svgrid.com/demos/322-form/

Install: `npm i @svgrid/grid` (Svelte 5).

## 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

- [SvForm](https://svgrid.com/docs/help/ui-components/sv-form.md)

## Related articles

- [Avoiding Layout Thrash in Custom Grid Cells](https://svgrid.com/blog/avoiding-layout-thrash-custom-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](https://svgrid.com/blog/throttle-live-updates-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)](https://svgrid.com/blog/stable-row-identity-getrowid/) - 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)

```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>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/322-form.svelte)
