Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

Binding number to input in Svelte and TypeScript

$
0
0

I have a form in svelte, using shadcn-svelte, and zod.

Relevant code snippets:

// schema.ts const formSchema = z.object({  /* ... */  maxParticipants: z.number()});
<!-- form.svelte --><Form.Field {form} name="maxParticipants"><Form.Control let:attrs><Form.Label>Max participants</Form.Label><Input {...attrs} type="number" bind:value={$formData.maxParticipants} /></Form.Control><Form.FieldErrors /></Form.Field>

Because inputs are strings, even though the input is set to type="number", it's a string, which means that when I try to submit, I get an error underneath the input (in the FieldErrors, from the zod validation): "Expected number, received string".

I couldn't find a way to go about it, so all I have currently is just setting the input to type="text", and .string() in the zod schema, and then in the actions, where I handle the form submission, cast it to a number, but that's would be very not optimal.


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>