SvelteKit Superforms with Zod: An Issue with Checkbox Missing from Form Data

I'm currently in the process of creating a form with SvelteKit Actions, Superforms, Formsnap, and Zod. However, I've encountered an issue with my checkbox not functioning as expected.

// schema.ts

export const formSchema = z.object({
    ...
    private: z.boolean().default(true),
    ...
});
<!-- +page.svelte -->

<script lang="ts">
    ...
    export let data: PageData;
    const form = superForm(data.form, {
        validators: zodClient(formSchema),
    });
    const { enhance, form: formData, errors, submitting } = form;
   ...
</script>
...
<Form.Field {form} name="private">
    <Form.Control let:attrs>
            <Form.Label>Private</Form.Label>
            <Checkbox {...attrs} bind:checked={$formData.private} />
    </Form.Control>
</Form.Field>
...

While things appear to be working fine on the client side (with the value displaying correctly in the SuperDebug component and updating accordingly), I noticed that upon form submission, the private field is missing from the form data (this was observed within the onSubmit event and on the server). As a result, the value defaults to false on the server side.

In an attempt to resolve this issue, I also tried using an

<input type="checkbox" />
instead of the custom <Checkbox /> component, but unfortunately, the problem persisted. It seems like the issue lies in the form submission process.

I'm puzzled as to why this is happening. Everything appears to be set up correctly, and it's working smoothly for all other text-based form fields I have implemented.

Answer №1

It appears that there is a crucial hidden field missing.

<Form.Field {form} name="secure">
<Form.Control let:attrs>
        <Form.Label>Secure</Form.Label>
        <Checkbox {...attrs} bind:checked={$formData.secure} />
        <!-- Ensure hidden field is included -->
        <input name={attrs.name} value={$formData.secure} hidden />
</Form.Control>
</Form.Field>

To learn more, refer to superforms documentation

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to identify browser version in Angular 4 to discourage IE usage

Is there a method in Angular 4 (TypeScript) for detecting the browser type? I am currently working with Angular 4 and would like to explore options for identifying the browser type when my application is loaded. I specifically want to prevent my applicati ...

Error handling in Angular is not properly managing the custom exception being thrown

I am currently working on an Angular 12 application and I have a requirement to implement a custom ErrorHandler for handling errors globally. When I receive an error notification from the backend, I subscribe to it in the ToolService using this.notificati ...

AngularJS2 brings a powerful and seamless implementation of indexedDB for efficient

I'm on the hunt for an indexeddb implementation that works seamlessly with Angularjs2. While I stumbled upon this api at https://github.com/gilf/angular2-indexeddb, it appears to be lacking in active development and may not be ready for production use ...

The ts-loader seems to be malfunctioning (It appears that a suitable loader is required to handle this file type, as no loaders are currently set up to process it)

I'm currently in the process of integrating TypeScript into a JavaScript project, but it seems like webpack is not recognizing the ts-loader for files with the .tsx extension. I've attempted to use babel and even tried awesome-ts-loader, but none ...

Specialized purpose for typed arrays

Given an array containing elements of two different entities: interface A { type: string } interface B { type: number } const a = {} as A const b = {} as B const array = [a, b] The array is of type (A | B)[] How can we create a utility type that ...

Filtering JSON data in Ionic 4 based on multiple values

Recently, I've encountered an issue with filtering a local JSON file based on multiple criteria. Initially, I thought that combining conditions using '&&' would solve the problem. However, when the data is loaded into Ngx-Datatable, nothing ...

Can you explain the process of type casting an object in TypeScript?

Looking at this example, I am pondering how to convert an Object into an interface (or a class): interface Person { firstName: string; lastName: string; } var obj={firstName:"James", lastName:"Bond"} as Person; console.log(type ...

Discovering a specific node within a tree structure in an Angular component

I have a tree object with nested nodes and IDs var data = [ { id: 'topNode', parameter: 'parameter', children: [ { id: 'node1', parameter: 'parameter', children: [ { id: ...

Angular AutoComplete feature does not accurately filter the list items

I need to implement an auto-complete feature for the county field due to a large number of items in the list causing inconvenience to users who have to scroll extensively. Currently, there are two issues with the code. The first problem is that although t ...

Sharing a Promise between Two Service Calls within Angular

Currently, I am making a service call to the backend to save an object and expecting a number to be returned via a promise. Here is how the call looks: saveTcTemplate(item: ITermsConditionsTemplate): ng.IPromise<number> { item.modifiedDa ...

Using Tailwind classes as a prop functions correctly, however, it does not work when directly applied

Here's a component snippet I'm working on: export const TextInput = ({ label, wrapperClassName = "", inputClassName = "", labelClassName = "", placeholder = "", ...props }: InputProps & Fiel ...

Angular 8 Issue: Absence of _body and Headers in Response

Our back-end code is written in C# within the .NET environment, targeting the 4.6.1 framework. Recently, our front-end was upgraded from Angular 4 to Angular 8. During this upgrade, webpack transitioned from version 2.3 to version 4.41 and typescript from ...

Exploring the Potential of Using ngIf-else Expressions in Angular 2

Here is a code snippet that I wrote: <tr *ngFor="let sample of data; let i = index" [attr.data-index]="i"> <ng-container *ngIf="sample.configuration_type == 1; then thenBlock; else elseBlock"></ng-container> <ng-template #t ...

Expanding function parameter types using intersection type in Typescript

As I delve into the world of intersection types to enhance a function with an incomplete definition, I encountered an interesting scenario. Take a look at this code snippet: WebApp.connectHandlers.use("/route", (req:IncomingMessage, res:ServerResponse)=& ...

Obtaining RestAPI data through axios in Vuex actions: a step-by-step guide

I attempted to utilize Axios in the actions of my Vuex system to retrieve data from a RestAPI and then display it in components. To test this out, I created a small app using a sample API, but encountered an error. The API I used can be found here. It con ...

Using TypeScript along with the "this" parameter

Hi there, I'm encountering an issue with the code snippet below. It keeps throwing an error message that says "Property 'weatherData' does not exist on type 'XMLHttpRequest'." The purpose of this code is to display weather informat ...

The 'checked' property cannot be bound to 'mat-button-toggle' as it is not recognized as a valid property in Angular 9

I am encountering an issue with my Angular 9 application. I have integrated angular-material and imported the MatCheckboxModule correctly in the module. Here is the version of the material package I am using: "@angular/material": "^10.2.0&q ...

Angular 5 offers the capability to use mat-slide-toggle to easily display and manipulate

I am experiencing an issue with displaying data in HTML using a mat-slide-toggle. The mat-slide-toggle works correctly, but the display does not reflect whether the value is 1 (checked) or 0 (unchecked). Can anyone provide some ideas on how to resolve this ...

Utilizing Dual Destructuring for Handling Undefined Main Objects

Before we proceed, I want to clarify that my question is not a duplicate of ES6 double destructure Let's examine the code snippet related to Apollo Client GraphQL: import { gql, useQuery, useMutation } from '@apollo/client'; ... const { loa ...

Issues with JSONPATH in typescript failing to grab any values

Searching for a specific config item validity using JSON path can be achieved by specifying the key name condition. This process works seamlessly on platforms like , accurately extracting the desired value: https://i.sstatic.net/2ffAAnNM.png In Typescrip ...