svecosystem/formsnap

Failed to validate a input field of type 'number' with a schema declared as a number.

alexanderscpo opened this issue · 3 comments

It has followed every step from https://www.shadcn-svelte.com/docs/components/form. The only difference is the declaration of the Zod schema and the data type for the text input. However, when submitting the form, it doesn't pass validation because the text input field converts the value to a string instead of maintaining it as a number.

const formSchema = z.object({
    budget: z.number()
})
<FormField {config} name="budget">
	<FormLabel>Presupuesto</FormLabel>
	<FormInput type="number" min="1"/>
        <Validation/>
</FormField>

Try:

const formSchema = z.object({
    budget: z.coerce.number()
})

Thanks, I didn't know about the coerce method. In the end I had solved it using a regular expression.

Yeah I think this is the optimal approach for handling this. We could do some proxy things but then it would become messy with needing <FormNumberInput /> etc!