Skip to content

Multiple forms on the same page #578

Open
@OliverBurrell

Description

@OliverBurrell

The example code on https://superforms.rocks/concepts/multiple-forms (with different form names) does not display messages anymore after Svelte 5 migration.
I had a similar page working in Svelte 4.2.7, Sveltekit-Superforms 2.19.1.

Here is the +page.server.ts:
import { z } from 'zod';
import { fail } from '@sveltejs/kit';
import { message, superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import type { Actions, PageServerLoad } from './$types';

const loginSchema = z.object({
email: z.string().email(),
password: z.string().min(8)
});

const registerSchema = z.object({
email: z.string().email(),
password: z.string().min(8),
confirmPassword: z.string().min(8)
});

export const load: PageServerLoad = async () => {
// Different schemas, no id required.
const loginForm = await superValidate(zod(loginSchema));
const registerForm = await superValidate(zod(registerSchema));

// Return them both
return { loginForm, registerForm };
};

export const actions = {
login: async ({ request }) => {
const loginForm = await superValidate(request, zod(loginSchema));

if (!loginForm.valid) return fail(400, { loginForm });

// TODO: Login user
return message(loginForm, 'Login form submitted');

},

register: async ({ request }) => {
const registerForm = await superValidate(request, zod(registerSchema));

if (!registerForm.valid) return fail(400, { registerForm });

// TODO: Register user
return message(registerForm, 'Register form submitted');

}
} satisfies Actions;

+page.svelte:

<script lang="ts"> import { superForm } from 'sveltekit-superforms/client'; let { data } = $props(); const { form, errors, enhance, message } = superForm(data.loginForm, { resetForm: true }); const { form: registerForm, errors: registerErrors, enhance: registerEnhance, message: registerMessage } = superForm(data.registerForm, { resetForm: true }); </script>

{#if $message}

{$message}

{/if}

E-mail: Password: Submit

{#if $registerMessage}

{$registerMessage}

{/if}

E-mail: Password: Confirm password: Submit

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions