# Forms that stay mounted

> Switch records, validate against props, and submit with the latest callbacks.

Source: https://forms.lunarhue.dev/solid/basics/long-lived-forms

Some forms outlive the props they were created with, like a detail pane that switches between customers or a transfer form whose parent passes a changing account balance or `onSave` callback.

`createForm` runs once when your component initializes. Because Solid props are reactive getters, any callback that reads `props.something` when invoked sees the latest value, which covers both validators and submit callbacks. Only switching between records needs a fresh form instance.

## Switching between records

Wrap your form component in a keyed `<Show>` so Solid creates a fresh component (and form) whenever the record changes:

```tsx
const customerForm = forms.define((saved: Customer) => ({
  defaultValues: saved,
  onSubmit: (value) => customers.save(value),
}));

function CustomerPane(props: { readonly customer: Customer }) {
  return (
    <Show when={props.customer} keyed>
      {(customer) => <CustomerForm saved={customer} />}
    </Show>
  );
}
```

Unsaved edits from the previous record go away with the old form. Keep in mind that a refetched copy of the *same* record is a new object reference too. If you want to absorb a fresh server copy without wiping the user's typing, keep the `<Show>` value stable and [rebase](https://forms.lunarhue.dev/solid/plugins/rebase.md) the form instead.

## Validating against live props

Since form definitions live outside components, attach prop-dependent rules inside your component with `createValidator` or the `validate` prop. Reading `props.balance` inside the check always sees the latest value:

```tsx
function TransferForm(props: { readonly balance: number }) {
  const form = createForm(transferForm);
  createValidator(form.root.at("amount"), (amount) =>
    amount > props.balance ? { message: "That's more than the balance." } : undefined,
  );
  // ...
}
```

Using `createValidator` on a field handle (or `form.Field`'s `validate` prop) ties the rule to that field's lifetime. Registering it on `form.root` in the form component keeps it active for the whole form, gives it the full form value, and puts its errors on the root.

A prop update alone doesn't trigger revalidation. If the error should update immediately when `props.balance` changes, call `form.root.at("amount").validate()` inside an effect that tracks `props.balance`.

## Calling the latest prop callback on submit

Pass a small wrapper function that reads `props.onSave` at call time, and your definition will always call the parent's latest callback:

```tsx
const customerForm = forms.define((input: { saved: Customer; save: (value: Customer) => Promise<void> }) => ({
  defaultValues: input.saved,
  onSubmit: (value) => input.save(value),
}));

function CustomerForm(props: CustomerFormProps) {
  const form = createForm(customerForm, { saved: props.saved, save: (value) => props.onSave(value) });
  // ...
}
```
