Skip to content

Instantly share code, notes, and snippets.

@stephane-vanraes
Last active June 25, 2023 00:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephane-vanraes/8ca9fece11c92f4d74e9282ac4b720f0 to your computer and use it in GitHub Desktop.
Save stephane-vanraes/8ca9fece11c92f4d74e9282ac4b720f0 to your computer and use it in GitHub Desktop.
Multi step forms with SvelteKit and actions
export const actions = {
first: async ({ request }) => {
const data = Object.fromEntries(await request.formData());
console.log('first', data);
return {
data,
step: 2
};
},
second: async ({ request }) => {
const data = Object.fromEntries(await request.formData());
console.log('second', data);
return {
data,
step: 3
};
},
third: async ({ request }) => {
const data = Object.fromEntries(await request.formData());
console.log('third', data);
// final step no return or whatever
}
};
<script>
import { enhance } from '$app/forms';
export let form;
$: form_so_far = form?.data ?? {};
$: step = form?.step ?? 1;
</script>
{#if step == 1}
<form method="POST" action="?/first" use:enhance>
<label>
<span>Name</span>
<input name="name" value="Rainlife" />
</label>
<button>Next</button>
</form>
{:else if step == 2}
<form method="POST" action="?/second" use:enhance>
{#each Object.entries(form_so_far) as [key, value]}
<input type="hidden" name={key} {value} />
{/each}
<label>
<span>Address</span>
<input name="addres" value="123" />
</label>
<button>Next</button>
</form>
{:else if step == 3}
<form method="POST" action="?/third" use:enhance>
{#each Object.entries(form_so_far) as [key, value]}
<input type="hidden" name={key} {value} />
{/each}
<label>
<span>Discord Handle</span>
<input name="discord" value="@Rainlife" />
</label>
<button>Next</button>
</form>
{/if}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment