Superforms, zod and server actions not running

I have an array of id’s, i would like to display a button for each id which when clicked submits the id to an action in my page.server.ts. It should be simple but i’m pulling my hair out trying to get it to work.

Minimum reproducable example

example.schemas.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { z } from "zod";
export const exampleSchema = z.object({
exampleId: z.string().length(24),
});
export type ExampleSchema = typeof exampleSchema;
</code>
<code>import { z } from "zod"; export const exampleSchema = z.object({ exampleId: z.string().length(24), }); export type ExampleSchema = typeof exampleSchema; </code>
import { z } from "zod";
 
export const exampleSchema = z.object({
  exampleId: z.string().length(24),
});
 
export type ExampleSchema = typeof exampleSchema;

example-component.svelte

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script lang="ts">
import { exampleSchema, type ExampleSchema } from "./example.schema";
import {
type SuperValidated,
type Infer,
superForm,
} from "sveltekit-superforms";
import SuperDebug from 'sveltekit-superforms';
import { zodClient } from "sveltekit-superforms/adapters";
export let data: SuperValidated<Infer<ExampleSchema>>;
const form = superForm(data, {
validators: zodClient(exampleSchema),
});
const { form: formData, enhance, message } = form;
let exampleArray = ["507f1f77bcf86cd799439011","507f1f77bcf86cd799439012","507f1f77bcf86cd799439013"]
</script>
<form method="POST" action="?/exampleAction" use:enhance>
{#each exampleArray as item}
<input type="hidden" name="exampleId" value="{item}"/>
<button type="submit" >{item}</button><br />
{/each}
</form>
<SuperDebug data={formData} />
</code>
<code><script lang="ts"> import { exampleSchema, type ExampleSchema } from "./example.schema"; import { type SuperValidated, type Infer, superForm, } from "sveltekit-superforms"; import SuperDebug from 'sveltekit-superforms'; import { zodClient } from "sveltekit-superforms/adapters"; export let data: SuperValidated<Infer<ExampleSchema>>; const form = superForm(data, { validators: zodClient(exampleSchema), }); const { form: formData, enhance, message } = form; let exampleArray = ["507f1f77bcf86cd799439011","507f1f77bcf86cd799439012","507f1f77bcf86cd799439013"] </script> <form method="POST" action="?/exampleAction" use:enhance> {#each exampleArray as item} <input type="hidden" name="exampleId" value="{item}"/> <button type="submit" >{item}</button><br /> {/each} </form> <SuperDebug data={formData} /> </code>
<script lang="ts">
    import { exampleSchema, type ExampleSchema } from "./example.schema";
    import {
        type SuperValidated,
        type Infer,
        superForm,
    } from "sveltekit-superforms";
    import SuperDebug from 'sveltekit-superforms';

    import { zodClient } from "sveltekit-superforms/adapters";

    export let data: SuperValidated<Infer<ExampleSchema>>;

    const form = superForm(data, {
        validators: zodClient(exampleSchema),
    });

    const { form: formData, enhance, message } = form;

    let exampleArray = ["507f1f77bcf86cd799439011","507f1f77bcf86cd799439012","507f1f77bcf86cd799439013"]

</script>

<form method="POST" action="?/exampleAction" use:enhance>
  {#each exampleArray as item}
    <input type="hidden" name="exampleId" value="{item}"/>
    <button type="submit" >{item}</button><br />
  {/each}
</form>
<SuperDebug data={formData} />

+page.server.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import type { PageServerLoad, Actions } from "./$types";
import { superValidate } from "sveltekit-superforms";
import { exampleSchema } from "$lib/components/custom/example.schema";
import { zod } from "sveltekit-superforms/adapters";
export const load: PageServerLoad = async () => {
return {
exampleForm: await superValidate(zod(exampleSchema)),
};
};
export const actions: Actions = {
exampleAction: async ({ request }) => {
const formData = await request.formData();
const exampleId = formData.get('exampleId');
// Process the ID as needed
console.log('Submitted ID:', exampleId);
return { success: true };
}
};
</code>
<code>import type { PageServerLoad, Actions } from "./$types"; import { superValidate } from "sveltekit-superforms"; import { exampleSchema } from "$lib/components/custom/example.schema"; import { zod } from "sveltekit-superforms/adapters"; export const load: PageServerLoad = async () => { return { exampleForm: await superValidate(zod(exampleSchema)), }; }; export const actions: Actions = { exampleAction: async ({ request }) => { const formData = await request.formData(); const exampleId = formData.get('exampleId'); // Process the ID as needed console.log('Submitted ID:', exampleId); return { success: true }; } }; </code>
import type { PageServerLoad, Actions } from "./$types";
import { superValidate } from "sveltekit-superforms";
import { exampleSchema } from "$lib/components/custom/example.schema";
import { zod } from "sveltekit-superforms/adapters";

export const load: PageServerLoad = async () => {
  return {
      exampleForm: await superValidate(zod(exampleSchema)),
  };
};

export const actions: Actions = {
    exampleAction: async ({ request }) => {    
      const formData = await request.formData();
      const exampleId = formData.get('exampleId');
  
      // Process the ID as needed
      console.log('Submitted ID:', exampleId);
  
      return { success: true };
    }
};

+page.svelte

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script lang="ts">
import type { PageData, ActionData } from './$types';
import Example from "$lib/components/custom/example-component.svelte";
export let data: PageData;
</script>
<Example data={data.exampleForm} />
</code>
<code><script lang="ts"> import type { PageData, ActionData } from './$types'; import Example from "$lib/components/custom/example-component.svelte"; export let data: PageData; </script> <Example data={data.exampleForm} /> </code>
<script lang="ts">
  import type { PageData, ActionData } from './$types';
  import Example from "$lib/components/custom/example-component.svelte";
  
  export let data: PageData;

</script>

<Example data={data.exampleForm} />

SuperDebug always shows the exampleId as empty and validation always fails, it doesn’t look like my action even runs at all.

The form is invalid because the exampleId is never transferred to the form data and thus the form is not submitted at all.

Quite a few things about this are odd:

  • You have one form and multiple inputs with the same name but only expect one value to be sent.
    You probably should send the the value directly via the button instead of using an input.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code><button
    type="submit"
    name="exampleId" value={item}>
    {item}
    </button>
    </code>
    <code><button type="submit" name="exampleId" value={item}> {item} </button> </code>
    <button
        type="submit"
        name="exampleId" value={item}>
        {item}
    </button>
    
  • Validation is meant for user inputs, there is little point in validating a hidden field with a fixed value. You could omit validation on the client-side, then the form should be valid and submit.
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>const form = superForm(data, {
    validators: zodClient(exampleSchema.omit({ exampleId: true })),
    });
    </code>
    <code>const form = superForm(data, { validators: zodClient(exampleSchema.omit({ exampleId: true })), }); </code>
    const form = superForm(data, {
        validators: zodClient(exampleSchema.omit({ exampleId: true })),
    });
    

    There is also an approach of setting the form data store property, which requires JS, e.g.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code><!-- only one input required -->
    <input type="hidden" name="exampleId" value={$formData.exampleId} />
    {#each exampleArray as item}
    <button
    type="submit"
    on:click={() => $formData.exampleId = item}>
    {item}
    </button>
    <br />
    {/each}
    </code>
    <code><!-- only one input required --> <input type="hidden" name="exampleId" value={$formData.exampleId} /> {#each exampleArray as item} <button type="submit" on:click={() => $formData.exampleId = item}> {item} </button> <br /> {/each} </code>
    <!-- only one input required -->
    <input type="hidden" name="exampleId" value={$formData.exampleId} />
    {#each exampleArray as item}
        <button
            type="submit"
            on:click={() => $formData.exampleId = item}>
            {item}
        </button>
        <br />
    {/each}
    
  • Validation on the server should be performed directly on the request and the validated form object should be returned according to docs.
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>const form = await superValidate(request, zod(exampleSchema));
    console.log('Submitted ID:', form.data.exampleId);
    return { form };
    </code>
    <code>const form = await superValidate(request, zod(exampleSchema)); console.log('Submitted ID:', form.data.exampleId); return { form }; </code>
    const form = await superValidate(request, zod(exampleSchema));
    
    console.log('Submitted ID:', form.data.exampleId);
    
    return { form };
    
  • (Form actions are bound to a page, putting the form in libs seems like it would likely lead to errors as the component is probably not compatible with most or any other pages.)

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật