Failed to validate a input field of type 'number' with a schema declared as a number.
alexanderscpo opened this issue · 3 comments
alexanderscpo commented
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>
BenocxX commented
Try:
const formSchema = z.object({
budget: z.coerce.number()
})
alexanderscpo commented
Thanks, I didn't know about the coerce method. In the end I had solved it using a regular expression.
huntabyte commented
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!