← Back to patterns

Multiple form instances

One form() per list item with .for(id)

Pattern Usage:

{#each await getItems() as item (item.id)}
  {@const edit = updateItem.for(item.id)}

  <form {...edit}>
    <input {...edit.fields.id.as('hidden', item.id)} />
    <input {...edit.fields.name.as('text', item.name)} />
    <input {...edit.fields.price.as('number', item.price)} />
    <button disabled={edit.pending > 0}>Save</button>
    {edit.result?.savedAt}
  </form>
{/each}

Live Demo:

Tip: save two rows quickly. Each instance has its own pending state, issues and result.

How it works: A single form() export normally has one shared state. updateItem.for(item.id) returns an isolated instance per key, with its own fields, issues, pending and result.

The second argument of .as(type, value) sets the initial value, which is also what the field falls back to on reset. Prices use .as('number'), so the schema receives a real number. The handler calls getItems().set(items) to update the list in the same response.