Saving and submitting

Saving and submitting

The form hands you the payload; where it goes is up to your app.

Two functions

FormView takes save and submit. Both are optional, and both get the same three things:

async function save(payload: Payload, values: FormValues, sends: Sends) {
  await api.put(`/drafts/${id}`, payload);
}

async function submit(payload: Payload, values: FormValues, sends: Sends): Promise<SubmitResult> {
  const reply = await api.post("/applications", payload);
  return { next: `/applications/${reply.id}` };
}
<FormView :form="form" title="Café licence" :save="save" :submit="submit" />
ArgumentWhat it is
payloadthe answers in the shape your API expects, built from paths
valuesthe answers as they are on screen, for an app that keeps its own copy
sendssections sent to their own API, by name

The engine never knows a URL. Where drafts live and which API gets the application is your app's choice.

When it saves

A wizard calls save when people press Continue on a valid step, when they go Back after changing something, on Save & leave, and once more just before submit. Files are uploaded with files.upload first, so the payload holds stored files, not raw ones.

save throws when it cannot save. The form stays on the step and shows the error, written by errorMessage.

Without save, nothing is saved between steps, which suits a form shown inside another page.

Submitting

Submit asks people to confirm, saves, then calls submit. It returns one of two things:

return { next: "/applications/42" };            // sent: show a toast and go here
return { errors: [{ field: "cafe.name", message: "This name is taken" }] };

Each error names a path in the payload. The form finds the field that sends to that path, opens its step and its section, and shows the message under it. An error that matches no field is shown in a toast.

Without submit there is no Submit button.

Leaving

back-to adds Back on the first step, and leave-to adds Save & leave. The form asks before people leave with answers it has not saved; :leave-guard="false" turns that off.